Python2和Python3之间的执行时间略有不同
最后,我写了一个简单的排列生成器,使用的是Python(实现了Knuth在《艺术... 4》中描述的“简单变化”算法)。我很好奇在Python2和Python3之间执行时间的差异。
这是我的函数:
def perms(s):
s = tuple(s)
N = len(s)
if N <= 1:
yield s[:]
raise StopIteration()
for x in perms(s[1:]):
for i in range(0,N):
yield x[:i] + (s[0],) + x[i:]
在Python3版本中,我只是把print x改成了print(x),因为在Python3中,print是一个函数。我使用timeit模块测试了这两个版本。
我的测试结果:
$ echo "python2.6:" && ./testing.py && echo "python3:" && ./testing3.py
Python2.6:
args time[ms]
1 0.003811
2 0.008268
3 0.015907
4 0.042646
5 0.166755
6 0.908796
7 6.117996
8 48.346996
9 433.928967
10 4379.904032
Python3:
args time[ms]
1 0.00246778964996
2 0.00656183719635
3 0.01419159912
4 0.0406293644678
5 0.165960511097
6 0.923101452814
7 6.24257639835
8 53.0099868774
9 454.540967941
10 4585.83498001
从结果来看,当参数数量少于6时,Python3的速度更快,但当参数数量增加后,情况就反过来了,Python2.6表现得更好。因为我还是Python编程的新手,我想知道这是为什么?或者说我的脚本可能更适合Python2?
提前感谢你的友好回答 :)
3 个回答
0
我觉得那些数字在统计上是没有意义的。
因为影响这些变化的因素太多了,所以它们根本不能代表什么重要的东西。
4
2
这个问题其实很有意思。
我使用了以下脚本,这个脚本可以在Python 2.6、2.7,以及3.0、3.1和3.2上运行。
from __future__ import print_function
from timeit import Timer
from math import factorial
try:
range = xrange
except:
pass
def perms(s):
s = tuple(s)
N = len(s)
if N <= 1:
yield s[:]
raise StopIteration()
for x in perms(s[1:]):
for i in range(0,N):
yield x[:i] + (s[0],) + x[i:]
def testcase(s):
for x in perms(s):
pass
def test():
for i in range(1,11):
s = "".join(["%d" % x for x in range(i)])
s = "testcase(\"%s\")" % s
t = Timer(s,"from __main__ import testcase")
factor = 100000
factor = int(factor/factorial(i))
factor = (factor>0) and factor or 1
yield (i,(1000*min(t.repeat(5,factor))/factor))
if __name__=="__main__":
print("args\ttime[ms]")
for x in test():
print("%i\t%f" % x)
我的平台是Ubuntu 10.10,64位,所有版本的Python都是从源代码编译的。我得到了以下结果:
case@quad:~$ py27 perms.py
args time[ms]
1 0.002221
2 0.005072
3 0.010352
4 0.027648
5 0.111339
6 0.618658
7 4.207046
8 33.213019
9 294.044971
10 2976.780891
case@quad:~$ py32 perms.py
args time[ms]
1 0.001725
2 0.004997
3 0.011208
4 0.032815
5 0.139474
6 0.761153
7 5.068729
8 39.760470
9 356.358051
10 3566.874027
经过一些实验,我发现性能差异主要出现在这一段代码:x[:i] + (s[0],) + x[i:]
。如果我在循环开始时只计算一次元组,并在每个yield语句中返回它,那么两个版本的Python运行速度是一样的。(虽然排列组合的结果是错的,但这不是重点。)
如果我单独测试那段代码,它的速度明显慢很多。
case@quad:~$ py27 -m timeit -s "s=(1,2,3,4,5);x=(1,2,3,4,5,6,7,8)" "x[:3] + (s[0],) + x[3:]"
1000000 loops, best of 3: 0.549 usec per loop
case@quad:~$ py32 -m timeit -s "s=(1,2,3,4,5);x=(1,2,3,4,5,6,7,8)" "x[:3] + (s[0],) + x[3:]"
1000000 loops, best of 3: 0.687 usec per loop
接下来,我使用dis.dis()来查看两个版本生成的字节码。
case@quad:~/src/Python-3.0.1$ py32
Python 3.2b2 (r32b2:87398, Dec 21 2010, 21:39:59)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import dis
>>> s=(1,2,3,4,5)
>>> x=(1,2,3,4,5,6,7,8)
>>> def f(s,x):
... return x[:3] + (s[0],) + x[3:]
...
>>> dis.dis(f)
2 0 LOAD_FAST 1 (x)
3 LOAD_CONST 0 (None)
6 LOAD_CONST 1 (3)
9 BUILD_SLICE 2
12 BINARY_SUBSCR
13 LOAD_FAST 0 (s)
16 LOAD_CONST 2 (0)
19 BINARY_SUBSCR
20 BUILD_TUPLE 1
23 BINARY_ADD
24 LOAD_FAST 1 (x)
27 LOAD_CONST 1 (3)
30 LOAD_CONST 0 (None)
33 BUILD_SLICE 2
36 BINARY_SUBSCR
37 BINARY_ADD
38 RETURN_VALUE
>>> exit()
case@quad:~/src/Python-3.0.1$ py26
Python 2.6.6 (r266:84292, Oct 24 2010, 15:27:46)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import dis
>>> s=(1,2,3,4,5)
>>> x=(1,2,3,4,5,6,7,8)
>>> def f(s,x):
... return x[:3] + (s[0],) + x[3:]
...
>>> dis.dis(f)
2 0 LOAD_FAST 1 (x)
3 LOAD_CONST 1 (3)
6 SLICE+2
7 LOAD_FAST 0 (s)
10 LOAD_CONST 2 (0)
13 BINARY_SUBSCR
14 BUILD_TUPLE 1
17 BINARY_ADD
18 LOAD_FAST 1 (x)
21 LOAD_CONST 1 (3)
24 SLICE+1
25 BINARY_ADD
26 RETURN_VALUE
>>>
这两个版本生成的字节码差别很大。不幸的是,我不知道为什么字节码会不同,所以我其实没有真正回答这个问题。不过,确实在切片和构建元组的性能上有显著差异。