python中while true_Python2中while 1比while True更快

1) bool类是从int类继承而来的

2) True/False 在python2中不是关键字,但是在python3是(True,False,None)

PS >python2

Enthought Canopy Python2.7.11 | 64-bit | (default, Jun 11 2016, 11:33:47) [MSC v.1500 64bit (AMD64)] on win32

Type"help", "copyright", "credits" or "license" formore information.>>> importkeyword>>>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

Python3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32bit (Intel)] on win32

Type"help", "copyright", "credits" or "license" formore information.>>> importkeyword>>>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值来执行

>>> delTrue>>> True+True2

>>> False +False

0>>>

4)While 1 比While True快

importtimeitdefwhile_one():

i=0while 1:

i+=1

if i==10000000:break

defwhile_true():

i=0whileTrue:

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的字节码

importdisdefwhile_one():while 1:pass

defwhile_true():whileTrue:pass

if __name__=="__main__":print "while_one \n"dis.dis(while_one)print "while_true \n"dis.dis(while_true)

结果:

while_one4 0 SETUP_LOOP 4 (to 7)5 >> 3 JUMP_ABSOLUTE 3

6POP_BLOCK>> 7LOAD_CONST 0 (None)10RETURN_VALUE

while_true8 0 SETUP_LOOP 10 (to 13)>> 3LOAD_GLOBAL 0 (True)6 POP_JUMP_IF_FALSE 12

9 9 JUMP_ABSOLUTE 3

>> 12POP_BLOCK>> 13LOAD_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)>>>t13.1002996925072743

>>>t23.077654023474544

5) if x==True or if x:

后者比前者快

#! python2#-*- coding:utf-8 -*-

importtimeitdefif_x_eq_true():

x=Trueif x==True:pass

defif_x():

x=Trueifx: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.186029813246if_x:0.120894725822

字节码:

importdisdefif_x_eq_true():

x=Trueif x==True:pass

defif_x():

x=Trueifx: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_true40 LOAD_GLOBAL 0 (True)3STORE_FAST 0 (x)5 6LOAD_FAST 0 (x)9LOAD_GLOBAL 0 (True)12 COMPARE_OP 2 (==)15 POP_JUMP_IF_FALSE 21

6 18 JUMP_FORWARD 0 (to 21)>> 21LOAD_CONST 0 (None)24RETURN_VALUE

if_x90 LOAD_GLOBAL 0 (True)3STORE_FAST 0 (x)10 6LOAD_FAST 0 (x)9 POP_JUMP_IF_FALSE 15

11 12 JUMP_FORWARD 0 (to 15)>> 15LOAD_CONST 0 (None)18 RETURN_VALUE

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值