python2和python3之间的执行时间略有不同

时间:2010-12-29 21:51:00

标签: python testing time python-3.x execution

最后,我在python中编写了一个简单的排列生成器(Knuth在“The Art ... 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),因为print是py3中的一个函数。 我使用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的参数数量,python 3更快,但是角色被反转并且python2.6做得更好。 因为我是python编程的新手,我想知道为什么会这样?或者我的脚本可能更适合python2?

提前感谢您的回答:)

3 个答案:

答案 0 :(得分:4)

Quoting

  

3.0的最终结果   概括就是Python 3.0   运行pystone基准约10%   比Python 2.5慢。最有可能的   最大的原因是去除   小整数的特殊外壳。   还有改进的余地,但它   将在3.0发布后发生!

>>> 4585.83498001/4379.904032
1.0470172283468882

所以你看到了5%的减速。引用的文字预计将减缓10%。所以我接受这是一个合理的放缓。

然而,正如herehere所见,情况有所改善。因此,如果您担心5%的减速,请尝试3.1或3.2。

答案 1 :(得分: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        
>>> 

两个版本之间生成的字节码非常不同。不幸的是,我不知道为什么字节码不同,所以我真的没有回答这个问题。但是切片和构建元组的性能确实存在显着差异。

答案 2 :(得分:0)

我肯定会称这些数字在统计上无关紧要。

对于那些真正具有任何意义的变体,有太多因素在起作用。

相关问题