Slightly different execution times between python2 and python3
- by user557634
Hi.
Lastly I wrote a simple generator of permutations in python (implementation of "plain changes" algorithm described by Knuth in "The Art... 4").
I was curious about the differences in execution time of it between python2 and python3.
Here is my function:
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:]
I tested both using timeit module.
My tests:
$ 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
As you can see, for number of arguments less than 6, python 3 is faster, but then roles are reversed and python2.6 does better.
As I am a novice in python programming, I wonder why is that so? Or maybe my script is more optimized for python2?
Thank you in advance for kind answer :)