python中not not x 与bool(x) 的区别
作者:迟业 时间:2021-04-27 03:50:17
他们都可以把 x 变成一个布尔类型的值:
>>> x = 123
>>> not not x
True
>>> bool(x)
True
>>>
那么谁更快呢?我们写段代码,跑个 100 万次,来比较下谁更快:
import timeit
def bool_convert(x):
return bool(x)
def notnot_convert(x):
return not not x
def main():
trials = 10_000_000
kwargs = {
"setup": "x=42",
"globals": globals(),
"number": trials,
}
notnot_time = timeit.timeit("notnot_convert(x)", **kwargs)
bool_time = timeit.timeit("bool_convert(x)", **kwargs)
print(f"{bool_time = :.04f}")
print(f"{notnot_time = :.04f}")
if __name__ == "__main__":
main()
运行结果如下:
其实 bool(x)
慢的原因在于它是一个函数调用,而 not not x
就是一条指令,具有更快捷的转换为布尔值的路径,这一点可以从字节码可以看出来:
bool(x)
多了 LOAD_GLOBAL
和 CALL_FUNCTION
。
这里附一下相关字节码的官方说明:
LOAD_GLOBAL(namei)
Loads the global named co_names[namei] onto the stack.
CALL_FUNCTION(argc)
Calls a callable object with positional arguments. argc indicates the number of positional arguments. The top of the stack contains positional arguments, with the right-most argument on top. Below the arguments is a callable object to call. CALL_FUNCTION pops all arguments and the callable object off the stack, calls the callable object with those arguments, and pushes the return value returned by the callable object.
UNARY_NOT
Implements TOS = not TOS.
最后:
从结果来看,not not x 比 bool(x)
更快,主要原因在于 bool(x)
是一个函数调用,函数调用需要参数压入栈顶,堆栈的顶部包含位置参数,最右边的参数在顶部,参数下面是要调用的可调用对象。CALL_FUNCTION
从堆栈中弹出所有参数和可调用对象,使用这些参数调用可调用对象,并推送可调用对象返回的返回值,这一过程比一个 not
指令要慢得多。
不过我仍然推荐你使用 bool(x)
,因为它的可读性更高,而且,我们也不太可能调用它 100万次。
来源:https://juejin.cn/post/7042544123416412167
标签:python,not,not,x,nbool(x)
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
np.concatenate()函数的具体使用
2023-06-21 11:15:19
python双向链表实现实例代码
2023-05-16 09:38:54
![](https://img.aspxhome.com/file/2023/8/99218_0s.jpg)
Python的三个重要函数详解
2022-05-19 04:58:45
![](https://img.aspxhome.com/file/2023/9/100859_0s.png)
深入SQL SERVER 2000的内存管理机制
2010-04-25 10:52:00
python学生信息管理系统(完整版)
2023-06-25 05:16:31
python for循环赋值问题
2023-01-26 05:56:32
Python使用Matplotlib实现雨点图动画效果的方法
2023-02-04 10:32:05
![](https://img.aspxhome.com/file/2023/3/105063_0s.gif)
Python安装与基本数据类型教程详解
2022-04-18 18:44:59
![](https://img.aspxhome.com/file/2023/5/120315_0s.jpg)
Python网页正文转换语音文件的操作方法
2021-03-26 04:27:30
![](https://img.aspxhome.com/file/2023/9/96999_0s.jpg)
永不熄灭的爱心图标——腾讯公益月捐计划 “QQ首席图标”诞生记
2009-09-01 19:43:00
![](https://img.aspxhome.com/file/UploadPic/20099/1/0909_loveplan_banner-44s.jpg)
Python实现绘制M2货币供应率曲线
2023-07-31 04:30:43
![](https://img.aspxhome.com/file/2023/1/77601_0s.jpg)
Python实现程序判断季节的代码示例
2022-04-04 13:30:22
![](https://img.aspxhome.com/file/2023/1/76941_0s.png)
简明 Python 基础学习教程
2023-01-05 13:02:05
css布局自适应高度方法
2007-05-11 17:03:00
将数据从MySQL迁移到 Oracle的注意事项
2008-12-03 15:41:00
详解Python如何批量检查图像是否可用
2021-10-03 13:42:07
ASP进阶学习之认识数学函数
2007-10-08 13:15:00
Python SVM(支持向量机)实现方法完整示例
2021-06-09 14:20:34
![](https://img.aspxhome.com/file/2023/7/75947_0s.png)
Python 爬虫学习笔记之多线程爬虫
2022-10-03 15:10:37
![](https://img.aspxhome.com/file/2023/8/95128_0s.png)
基于python和flask实现http接口过程解析
2022-06-01 11:46:36