python while-Python天坑系列(一):while 1比while True更快?
更多
0. 前言
前些天被Python的多線程坑了一把,因此產(chǎn)生了寫一個(gè)《Python天坑系列》博客的想法,說說我碰到的那些Python的坑。
而天坑這個(gè)詞呢,一方面指Python的坑,另一方面也說明本系列文章也是個(gè)坑,對于會寫什么內(nèi)容、有多少篇、多久更新一次、什么時(shí)間更新我都無法確定,哈哈(看,之前已經(jīng)3個(gè)月沒有更新過了!)。
本篇是系列的第一篇,講的內(nèi)容是Python的bool類型。
1. 前提
1.1 bool是int的子類
根據(jù)PEP285中Review部分第6條所述,bool類是從int類繼承而來的,這樣可以極大的簡化實(shí)現(xiàn)(C代碼中調(diào)用PyInt_Check()的地方仍將繼續(xù)工作)。
1.2 Python2中True/False不是關(guān)鍵字,但Python3中是
我們可以導(dǎo)入keyword模塊,來查看關(guān)鍵字:
keyword
Python
# Python2 關(guān)鍵字
>>> 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"]
1
2
3
4
5
# Python2 關(guān)鍵字
>>>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"]
而在Python3中,關(guān)鍵字中添加了True/False/None。
由于Python2中True/False不是關(guān)鍵字,因此我們可以對其進(jìn)行任意的賦值:
Python2 True賦值
Python
>>> (1 == 1) == True
True
>>> True = "pythoner.com"
>>> (1 == 1) == True
False
1
2
3
4
5
>>>(1==1)==True
True
>>>True="pythoner.com"
>>>(1==1)==True
False
2. True + True = 2
由于bool是繼承自int的子類,因此為了保證向下兼容性,在進(jìn)行算術(shù)運(yùn)算中,True/False會被當(dāng)作int值來執(zhí)行。
True + True = 2
Python
>>> True + True
2
>>> True - True
0
>>> True * True
1
>>> (True + True) > 1
True
>>> True + 5
6
>>> 1 / False
Traceback (most recent call last):
File "", line 1, in
ZeroDivisionError: integer division or modulo by zero
1
2
3
4
5
6
7
8
9
10
11
12
13
14
>>>True+True
2
>>>True-True
0
>>>True*True
1
>>>(True+True)>1
True
>>>True+5
6
>>>1/False
Traceback(mostrecentcalllast):
File"",line1,in
ZeroDivisionError:integerdivisionormodulobyzero
3. While 1比While True快?
首先來看一個(gè)比較while 1和while True循環(huán)的腳本,兩個(gè)函數(shù)中,除了1和True的區(qū)別之外,其他地方完全相同。
while 1與while True比較
Python
#! /usr/bin/python
# -*- coding: utf-8 -*-
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__":
w1 = timeit.timeit(while_one, "from __main__ import while_one", number=3)
wt = timeit.timeit(while_true, "from __main__ import while_true", number=3)
print "while one: %s while_true: %s" % (w1, wt)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#! /usr/bin/python
# -*- coding: utf-8 -*-
importtimeit
defwhile_one():
i=0
while1:
i+=1
ifi==10000000:
break
defwhile_true():
i=0
whileTrue:
i+=1
ifi==10000000:
break
if__name__=="__main__":
w1=timeit.timeit(while_one,"from __main__ import while_one",number=3)
wt=timeit.timeit(while_true,"from __main__ import while_true",number=3)
print"while one: %s while_true: %s"%(w1,wt)
執(zhí)行結(jié)果:
while one: 1.37000703812
while_true: 2.07638716698
可以看出wihle 1的執(zhí)行時(shí)間約為while True的2/3。
那么,這是為什么呢?
其實(shí)這就是前提中提到的關(guān)鍵字的問題。由于Python2中,True/False不是關(guān)鍵字,因此我們可以對其進(jìn)行任意的賦值,這就導(dǎo)致程序在每次循環(huán)時(shí)都需要對True/False的值進(jìn)行檢查;而對于1,則被程序進(jìn)行了優(yōu)化,而后不會再進(jìn)行檢查。
我們可以通過dis模塊來查看while_one和while_true的字節(jié)碼,下面的程序是對剛才的程序進(jìn)行了一定的簡化后的版本。
while 1和while True的字節(jié)碼程序
Python
#! /usr/bin/python
# -*- coding: utf-8 -*-
import dis
def while_one():
while 1:
pass
def while_true():
while True:
pass
if __name__ == "__main__":
print "while_one "
dis.dis(while_one)
print "while_true "
dis.dis(while_true)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#! /usr/bin/python
# -*- coding: utf-8 -*-
importdis
defwhile_one():
while1:
pass
defwhile_true():
whileTrue:
pass
if__name__=="__main__":
print"while_one "
dis.dis(while_one)
print"while_true "
dis.dis(while_true)
執(zhí)行的結(jié)果是:
while 1和while True的字節(jié)碼執(zhí)行結(jié)果
Python
while_one
6 0 SETUP_LOOP 3 (to 6)
7 >> 3 JUMP_ABSOLUTE 3
>> 6 LOAD_CONST 0 (None)
9 RETURN_VALUE
while_true
10 0 SETUP_LOOP 10 (to 13)
>> 3 LOAD_GLOBAL 0 (True)
6 POP_JUMP_IF_FALSE 12
11 9 JUMP_ABSOLUTE 3
>> 12 POP_BLOCK
>> 13 LOAD_CONST 0 (None)
16 RETURN_VALUE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
while_one
60SETUP_LOOP3(to6)
7>>3JUMP_ABSOLUTE3
>>6LOAD_CONST0(None)
9RETURN_VALUE
while_true
100SETUP_LOOP10(to13)
>>3LOAD_GLOBAL0(True)
6POP_JUMP_IF_FALSE12
119JUMP_ABSOLUTE3
>>12POP_BLOCK
>>13LOAD_CONST0(None)
16RETURN_VALUE
可以看出,正如上面所講到的,在while True的時(shí)候,字節(jié)碼中多出了幾行語句,正是這幾行語句進(jìn)行了True值的檢查。
而在Python3中,由于True/False已經(jīng)是關(guān)鍵字了,不允許進(jìn)行重新賦值,因此,其執(zhí)行結(jié)果與while 1不再有區(qū)別(好吧,我這沒有Python3的環(huán)境,就不去驗(yàn)證了,網(wǎng)上有人驗(yàn)證過了)。但是由于Python2的使用十分廣泛,因此大家不得不注意這個(gè)可能會降低性能的地方。
4. if x == True: 還是 if x:
在PEP285中,還提到了這兩種寫法的比較。PEP285中認(rèn)為,==具有傳遞性,a==b, b==c會被化簡為a==c。也就是說,如果選擇前一種寫法的話,6和7在if語句中都應(yīng)該被認(rèn)為是真值,那么就會造成6==True==7,被化簡為6==7的問題,因此后一種寫法才是正確的。
現(xiàn)在,讓我們偏個(gè)題,假設(shè)x就是True,那么程序的執(zhí)行效率又如何呢?
if x == True:和if x:比較
Python
#! /usr/bin/python
# -*- 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__":
if1 = timeit.timeit(if_x_eq_true, "from __main__ import if_x_eq_true", number = 1000000)
if2 = timeit.timeit(if_x, "from __main__ import if_x", number = 1000000)
print "if_x_eq_true: %s if_x: %s" % (if1, if2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#! /usr/bin/python
# -*- coding: utf-8 -*-
importtimeit
defif_x_eq_true():
x=True
ifx==True:
pass
defif_x():
x=True
ifx:
pass
if__name__=="__main__":
if1=timeit.timeit(if_x_eq_true,"from __main__ import if_x_eq_true",number=1000000)
if2=timeit.timeit(if_x,"from __main__ import if_x",number=1000000)
print"if_x_eq_true: %s if_x: %s"%(if1,if2)
執(zhí)行結(jié)果是:
if_x_eq_true: 0.212558031082
if_x: 0.144327878952
讓我們再來看看字節(jié)碼(程序未作修改,dis的使用方式同上,因此不再給出程序):
if x == True:和if x:的字節(jié)碼
Python
if_x_eq_true
8 0 LOAD_GLOBAL 0 (True)
3 STORE_FAST 0 (x)
9 6 LOAD_FAST 0 (x)
9 LOAD_GLOBAL 0 (True)
12 COMPARE_OP 2 (==)
15 POP_JUMP_IF_FALSE 21
10 18 JUMP_FORWARD 0 (to 21)
>> 21 LOAD_CONST 0 (None)
24 RETURN_VALUE
if_x
13 0 LOAD_GLOBAL 0 (True)
3 STORE_FAST 0 (x)
14 6 LOAD_FAST 0 (x)
9 POP_JUMP_IF_FALSE 15
15 12 JUMP_FORWARD 0 (to 15)
>> 15 LOAD_CONST 0 (None)
18 RETURN_VALUE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
if_x_eq_true
80LOAD_GLOBAL0(True)
3STORE_FAST0(x)
96LOAD_FAST0(x)
9LOAD_GLOBAL0(True)
12COMPARE_OP2(==)
15POP_JUMP_IF_FALSE21
1018JUMP_FORWARD0(to21)
>>21LOAD_CONST0(None)
24RETURN_VALUE
if_x
130LOAD_GLOBAL0(True)
3STORE_FAST0(x)
146LOAD_FAST0(x)
9POP_JUMP_IF_FALSE15
1512JUMP_FORWARD0(to15)
>>15LOAD_CONST0(None)
18RETURN_VALUE
可以清晰的看到第9行比第14行,多出了檢查True值和進(jìn)行比較的操作。
也就是說,不論從遵循PEP的規(guī)范,還是執(zhí)行效率,或者程序的簡潔性來說,我們都應(yīng)該使用if x:,而不是if x == True:來進(jìn)行比較。同理,那些if x is not None:之類的語句也應(yīng)當(dāng)被簡化為if x:(如果要比較的是非值,而不必須是None的話)。
5. References
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的python while-Python天坑系列(一):while 1比while True更快?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: ssm三大框架工作原理_蒸发器最常见的三
 - 下一篇: android gradle权威指南pd