|
|
|
|
|
by ldng
5484 days ago
|
|
$ python3.2 -m timeit "sum(map(lambda x: x3, range(10000)))" 100 loops, best of 3: 7.61 msec per loop $ python3.2 -m timeit "sum([ x3 for x in range(10000) ])" 100 loops, best of 3: 6.27 msec per loop $ python2.7 -m timeit "sum(map(lambda x: x3, range(10000)))" 100 loops, best of 3: 2.81 msec per loop $ python2.7 -m timeit "sum([ x3 for x in range(10000) ])" 100 loops, best of 3: 2.28 msec per loop I wasn't expecting python3 to be that slower. |
|
I infer that ldng has a 64 bit machine. On my 32 bit machine, Python 3.1 is faster than 2.6 for these examples. On a 64 bit machine I get similar results to ldng's, with Python3 being slower. If I wrap long() around the x in the example, Python2 becomes as slow as Python3.
Note that taking the cube of lots of big integers is not typical for many people: it generates very large integers that have to be in Python2's special long type on a 32 bit machine. On a 64 bit machine they stay as normal ints in Python2, which are much faster. Python3 has a single automagic int type, which seems to internally convert to the arbitrary precision type sooner than it has to on 64 bit machines(?).
Examples more typical of my use would wrap float() around the x, or change the example to add up 3x instead of x^3. These examples are all faster in Python3 for me. Faster still is to use numpy (which is now supported in Python3).
Summary: the people who would be affected by this regression have both a 64 bit machine, and do a lot of exact integer arithmetic on integers that can be represented in 64 bits, but not 32.