Python2中while 1比while True更快
1) bool类是从int类继承而来的
2) True/False 在python2中不是关键字,但是在python3是(True,False,None)
PS > python2
Enthought Canopy Python 2.7.11 | 64-bit | (default, Jun 11 2016, 11:33:47) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import keyword
>>> keyword.kwlist
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for',
'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while',
'with', 'yield']
>>> PS > python3
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', '
finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'retu
rn', 'try', 'while', 'with', 'yield']
>>>
由于Python2中True/False不是关键字,因此我们可以对其进行任意的赋值
>>> True="test"
>>> True
'test'
>>> "test"==True
True
>>> 1==True
False
>>>
3) 由于bool是继承自int的子类,因此为了保证向下兼容性,在进行算术运算中,True/False会被当作int值来执行
>>> del True
>>> True+True
2
>>> False + False
0
>>>
4)While 1 比While True快
import timeit def while_one():
i=0
while 1:
i+=1
if i==10000000:
break def while_true():
i=0
while True:
i+=1
if i==10000000:
break if __name__=="__main__":
t1=timeit.timeit(while_one,"from __main__ import while_one",number=3)
t2=timeit.timeit(while_true,"from __main__ import while_true", number=3)
#t1=timeit.timeit(while_one,"from __main__ import while_one",number=3)
print "while one : %s \nwhile true: %s " % (t1,t2) while one : 1.16112167105
while true: 1.66502957924
原因就是前提中提到的关键字的问题。由于Python2中,True/False不是关键字,因此我们可以对其进行任意的赋值,这就导致程序在每次循环时都需要对True/False的值进行检查;而对于1,则被程序进行了优化,而后不会再进行检查。
我们可以通过dis模块来查看while_one和while_true的字节码
import dis def while_one():
while 1:
pass def while_true():
while True:
pass if __name__=="__main__":
print "while_one \n"
dis.dis(while_one) print "while_true \n"
dis.dis(while_true)
结果:
while_one 4 0 SETUP_LOOP 4 (to 7) 5 >> 3 JUMP_ABSOLUTE 3
6 POP_BLOCK
>> 7 LOAD_CONST 0 (None)
10 RETURN_VALUE
while_true 8 0 SETUP_LOOP 10 (to 13)
>> 3 LOAD_GLOBAL 0 (True)
6 POP_JUMP_IF_FALSE 12 9 9 JUMP_ABSOLUTE 3
>> 12 POP_BLOCK
>> 13 LOAD_CONST 0 (None)
16 RETURN_VALUE
可以看出,正如上面所讲到的,在while True的时候,字节码中多出了几行语句,正是这几行语句进行了True值的检查
而在Python3中,由于True/False已经是关键字了,不允许进行重新赋值,因此,其执行结果与while 1不再有区别
PS > python3
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from test_while import *
>>> t1=timeit.timeit(while_one,"from __main__ import while_one",number=3)
>>> t2=timeit.timeit(while_true,"from __main__ import while_true", number=3)
>>> t1
3.1002996925072743
>>> t2
3.077654023474544
5) if x==True or if x:
后者比前者快
#! python2
#-*- coding:utf-8 -*- import timeit def if_x_eq_true():
x=True
if x==True:
pass def if_x():
x=True
if x:
pass if __name__=="__main__":
t1=timeit.timeit(if_x_eq_true,"from __main__ import if_x_eq_true", number=1000000)
t2=timeit.timeit(if_x,"from __main__ import if_x",number=1000000)
print "if_x_eq_true: %s \n if_x: %s" % (t1,t2) if_x_eq_true: 0.186029813246
if_x: 0.120894725822
字节码:
import dis def if_x_eq_true():
x=True
if x==True:
pass def if_x():
x=True
if x:
pass if __name__=="__main__":
print "if_x_eq_true \n"
dis.dis(if_x_eq_true) print "if_x \n"
dis.dis(if_x)
结果
if_x_eq_true 4 0 LOAD_GLOBAL 0 (True)
3 STORE_FAST 0 (x) 5 6 LOAD_FAST 0 (x)
9 LOAD_GLOBAL 0 (True)
12 COMPARE_OP 2 (==)
15 POP_JUMP_IF_FALSE 21 6 18 JUMP_FORWARD 0 (to 21)
>> 21 LOAD_CONST 0 (None)
24 RETURN_VALUE
if_x 9 0 LOAD_GLOBAL 0 (True)
3 STORE_FAST 0 (x) 10 6 LOAD_FAST 0 (x)
9 POP_JUMP_IF_FALSE 15 11 12 JUMP_FORWARD 0 (to 15)
>> 15 LOAD_CONST 0 (None)
18 RETURN_VALUE
Python2中while 1比while True更快的更多相关文章
- SharePoint 2010中使用SPListItemCollectionPosition更快的结果
转:http://www.16kan.com/article/detail/318657.html Introduction介绍 In this article we will explore the ...
- 详解:Python2中的urllib、urllib2与Python3中的urllib以及第三方模块requests
在python2中,urllib和urllib2都是接受URL请求的相关模块,但是提供了不同的功能.两个最显著的不同如下: 1.urllib2可以接受一个Request类的实例来设置URL请求的hea ...
- Python2中生成时间戳(Epoch,或Timestamp)的常见误区
在Python2中datetime对象没有timestamp方法,不能很方便的生成epoch,现有方法没有处理很容易导致错误.关于Epoch可以参见时区与Epoch 0 Python中生成Epoch ...
- Python2 中字典实现的分析【翻译】
在这片文章中会介绍 Python2 中字典的实现,Hash 冲突的解决方法以及在 C 语言中 Python 字典的具体结构,并分析了数据插入和删除的过程.翻译自python-dictionary-im ...
- for (;;) 与 while (true),哪个更快?
Java技术栈 www.javastack.cn 优秀的Java技术公众号 在 JDK8u 的 jdk 项目下做个很粗略的搜索: mymbp:/Users/me/workspace/jdk8u/jdk ...
- Java 里的 for (;;) 与 while (true),哪个更快?
在 JDK8u 的 jdk 项目下做个很粗略的搜索: mymbp:/Users/me/workspace/jdk8u/jdk/src$ egrep -nr "for \\(\\s?;\\s? ...
- 对于Java中的Loop或For-each,哪个更快
Which is Faster For Loop or For-each in Java 对于Java中的Loop或Foreach,哪个更快 通过本文,您可以了解一些集合遍历技巧. Java遍历集合有 ...
- 翻译:让网络更快一些——最小化浏览器中的回流(reflow)
关于reflowreflow(英音:[ri:’fləu] 美音:[ri’flo])在词典中的解释是回流,逆流.而在web应用中,翻译为回流有些牵强.我个人觉得,理解为回炉(重新塑形),似乎更加形象一点 ...
- python2中的__init__.py文件的作用
python2中的__init__.py文件的作用: 1.python的每个模块的包中,都必须有一个__init__.py文件,有了这个文件,我们才能导入这个目录下的module. 2.__init_ ...
随机推荐
- centos 安装cacti监控
CentOS 6下Cacti搭建文档 安装依赖关系 yum -y install mysql-devel httpd php php-pdo php-snmp php-mysql lm_sensors ...
- docker命令和后台参数
Docker官方为了让用户快速了解Docker,提供了一个 交互式教程 ,旨在帮助用户掌握Docker命令行的使用方法. Docker 命令行 下面对Docker的命令清单进行简单的介绍,详细内容在后 ...
- 基于jQuery弹性展开收缩菜单插件gooey.js
首先 引入css <link rel="stylesheet" href="css/bootstrap.min.css"> <link rel ...
- linux安装包资源库
最近发现了一个很不错的linux的rpm资源库,可以在里面找到rpm安装过程中缺失的资源! 网址:http://pkgs.org/
- How to solve java.net.SocketTimeoutException:60000millis problem in HDFS
Many HDFS users encounter the following error when DFSClient ready file from a certain Data Node. & ...
- meta常用标签总结
meta元素共有三个可选属性(http-equiv.name和scheme)和一个必选属性(content),content定义与 http-equiv 或 name 属性相关的元信息 可选属性 属性 ...
- gRPC编码初探(java)
背景:gRPC是一个高性能.通用的开源RPC框架,其由Google主要面向移动应用开发并基于HTTP/2协议标准而设计,基于ProtoBuf(Protocol Buffers)序列化协议开发,且支持众 ...
- iOS开发app自动更新的实现
#define kStoreAppId @“xxxxxxxxx” // (appid数字串) -(void)checkAppUpdate { NSDictionary *infoDict = [[NS ...
- Spring Security(12)——Remember-Me功能
目录 1.1 概述 1.2 基于简单加密token的方法 1.3 基于持久化token的方法 1.4 Remember-Me相关接口和实现类 1.4.1 Toke ...
- linux centOS 安装oracle
安装环境 Linux服务器:CentOS6.4-64位 oracle服务器:oracle11g-64位 基本要求 内存大小:至少2G 硬盘大小:至少6G 交换空间:一般为内存的2倍,例如:2G的内存可 ...