Python3: on removing map, reduce, filter

David M. Cooke cookedm+news at physics.mcmaster.ca
Mon Jan 10 22:57:41 EST 2005


Steven Bethard <steven.bethard at gmail.com> writes:
> Some timings to verify this:
>
> $ python -m timeit -s "def square(x): return x*x" "map(square, range(1000))"
> 1000 loops, best of 3: 693 usec per loop
>
> $ python -m timeit -s "[x*x for x in range(1000)]"
> 10000000 loops, best of 3: 0.0505 usec per loop

Maybe you should compare apples with apples, instead of oranges :-)
You're only running the list comprehension in the setup code...

$ python2.4 -m timeit -s "def square(x): return x*x" "map(square, range(1000))"
1000 loops, best of 3: 464 usec per loop
$ python2.4 -m timeit "[x*x for x in range(1000)]"
1000 loops, best of 3: 216 usec per loop

So factor of 2, instead of 13700 ...

-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca



More information about the Python-list mailing list