如何使用 Python 运算符进行性能优化 All In One

xgqfrms / 2023-08-06 / 原文

如何使用 Python 运算符进行性能优化 All In One

为什么 Python 运算符 // 比 运算符 / 性能更好,运行速度更快呀❓

demos

class Solution:
  def numberOfSteps(self, num: int) -> int:
    steps: int = 0
    while num > 0:
      steps += 1
      # num % 2 == 1
      if(num % 2):
        # 为什么 Python 运算符 // 比 运算符 / 性能更好,运行速度更快呀❓
        num -= 1
      else:
        num //= 2
    return steps

""" 

Runtime
Details
36ms
Beats 93.99%of users with Python3

Memory
Details
15.97mb
Beats 99.95%of users with Python3

"""

image

https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/submissions/

Python 算术运算符

image

https://www.runoob.com/python3/python3-basic-operators.html

refs



©xgqfrms 2012-2021

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!