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更快的更多相关文章

  1. SharePoint 2010中使用SPListItemCollectionPosition更快的结果

    转:http://www.16kan.com/article/detail/318657.html Introduction介绍 In this article we will explore the ...

  2. 详解:Python2中的urllib、urllib2与Python3中的urllib以及第三方模块requests

    在python2中,urllib和urllib2都是接受URL请求的相关模块,但是提供了不同的功能.两个最显著的不同如下: 1.urllib2可以接受一个Request类的实例来设置URL请求的hea ...

  3. Python2中生成时间戳(Epoch,或Timestamp)的常见误区

    在Python2中datetime对象没有timestamp方法,不能很方便的生成epoch,现有方法没有处理很容易导致错误.关于Epoch可以参见时区与Epoch 0 Python中生成Epoch ...

  4. Python2 中字典实现的分析【翻译】

    在这片文章中会介绍 Python2 中字典的实现,Hash 冲突的解决方法以及在 C 语言中 Python 字典的具体结构,并分析了数据插入和删除的过程.翻译自python-dictionary-im ...

  5. for (;;) 与 while (true),哪个更快?

    Java技术栈 www.javastack.cn 优秀的Java技术公众号 在 JDK8u 的 jdk 项目下做个很粗略的搜索: mymbp:/Users/me/workspace/jdk8u/jdk ...

  6. Java 里的 for (;;) 与 while (true),哪个更快?

    在 JDK8u 的 jdk 项目下做个很粗略的搜索: mymbp:/Users/me/workspace/jdk8u/jdk/src$ egrep -nr "for \\(\\s?;\\s? ...

  7. 对于Java中的Loop或For-each,哪个更快

    Which is Faster For Loop or For-each in Java 对于Java中的Loop或Foreach,哪个更快 通过本文,您可以了解一些集合遍历技巧. Java遍历集合有 ...

  8. 翻译:让网络更快一些——最小化浏览器中的回流(reflow)

    关于reflowreflow(英音:[ri:’fləu] 美音:[ri’flo])在词典中的解释是回流,逆流.而在web应用中,翻译为回流有些牵强.我个人觉得,理解为回炉(重新塑形),似乎更加形象一点 ...

  9. python2中的__init__.py文件的作用

    python2中的__init__.py文件的作用: 1.python的每个模块的包中,都必须有一个__init__.py文件,有了这个文件,我们才能导入这个目录下的module. 2.__init_ ...

随机推荐

  1. chrome下li标签onclick事件无效

    //绑定事件 $(document).ready(function () { $("ul").children().click(function () { clickLi(this ...

  2. jquery常用的选择器

    jquery用选择器来得到jquery对象,进而进行一些操作. 一.基本选择器 1.id选择器.示例:选择id为one的元素 var $one = $("#one"); 2.类选择 ...

  3. Webstrom 常用操作记录

    WebStorm 编译 es6 与 scss 的教程: http://blog.jetbrains.com/webstorm/2015/05/ecmascript-6-in-webstorm-tran ...

  4. css display属性介绍

    none此元素不会被显示. block此元素将显示为块级元素,此元素前后会带有换行符. inline默认.此元素会被显示为内联元素,元素前后没有换行符. inline-block行内块元素.(CSS2 ...

  5. linux的命令使用记录

    iptables禁止53端口的出包(dns) iptables -A OUTPUT -p udp --dport 53 -j DROP linux查看网络监听端口 netstat -npl 文件复制 ...

  6. Spring Aop详尽教程

    一.概念 AOP(Aspect Oriented Programming):面向切面编程. 面向切面编程(也叫面向方面编程),是目前软件开发中的一个热点,也是Spring框架中的一个重要内容.利用AO ...

  7. PAT 团体程序设计天梯赛-练习集L1-011. A-B

    本题要求你计算A-B.不过麻烦的是,A和B都是字符串 —— 即从字符串A中把字符串B所包含的字符全删掉,剩下的字符组成的就是字符串A-B. 输入格式: 输入在2行中先后给出字符串A和B.两字符串的长度 ...

  8. ubuntu环境下jdk安装及jenkins安装

    本文内容参考http://jingyan.baidu.com/article/c33e3f48a3365dea15cbb5c9.html 1 jdk下载 安装 http://www.oracle.co ...

  9. HTML5 学习总结

    1,h5比原来的h4.0版本的页面头部更为简化, <!doctype html> <meta charset="utf-8"/>sublime中快速生成格式 ...

  10. SecureCRT 7.3.4破解版(含注册机)

    不用说你肯定知道SecureCRT用途是什么,这个号称最好用的ssh连接工具却不是免费的,所以找了很久才找到最新版本的SecureCRT 7.3.4破解版,其实只要是SecureCRT 7.3.x版本 ...