Python2中while 1比while True更快

 

1) bool類是從int類繼承而來的python

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不是關鍵字,所以咱們能夠對其進行任意的賦值spa

>>> True="test"
>>> True
'test'
>>> "test"==True
True
>>> 1==True
False
>>>

 

3) 因爲bool是繼承自int的子類,所以爲了保證向下兼容性,在進行算術運算中,True/False會被看成int值來執行code

 

>>> del True
>>> True+True
2
>>> False + False
0
>>>

 

4)While 1 比While True快orm

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,則被程序進行了優化,然後不會再進行檢查。blog

 

咱們能夠經過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)
    

結果:utf-8

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值的檢查it

而在Python3中,因爲True/False已是關鍵字了,不容許進行從新賦值,所以,其執行結果與while 1再也不有區別io

 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        
相關文章
相關標籤/搜索