为什么python中的“ num == 0”比“ not num”快? [关闭]

问题描述 投票:0回答:1
我在玩leetcode时突然发现以下表达式:

if num == 0: count += 1

比…快

if not num: count += 1

我更喜欢第二个表达式,因为它对我来说很简单,但是为什么第一个表达式执行得比第二个表达式快?其他语言(C ++,Java,JS ...)的情况如何?

谢谢。

python programming-languages execution-time boolean-operations leetcode
1个回答
0
投票
根据我的测试,它肯定不是更快,所涉及的变量可能会使它歪斜。这么小的差异在现实世界中不重要。
© www.soinside.com 2019 - 2024. All rights reserved.