|
|
|
|
|
by imurray
5484 days ago
|
|
When times are that short you should question if Python is actually doing anything, or just promising to do it later (remember that range is a generator in python3). $ python3.1 -m timeit "map(lambda x: x**3, range(10000))"
1000000 loops, best of 3: 1.01 usec per loop
Seems fishy. Adding up the elements forces Python to actually do the cubing on all of them: $ python3.1 -m timeit "sum(map(lambda x: x**3, range(10000)))"
100 loops, best of 3: 13 msec per loop
$ python3.1 -m timeit "sum([ x**3 for x in range(10000) ])"
100 loops, best of 3: 10.6 msec per loop
map really is slower, at least for me in python3.1. |
|
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.