Integer From A Float List?!?

Peter Otten __peter__ at web.de
Sat Mar 5 08:35:58 CET 2005


Nick Coghlan wrote:

> C:\>python -m timeit -s "floats = map(float, range(1000))" "ints =
> map(int, floa ts)"
> 1000 loops, best of 3: 481 usec per loop
> 
> C:\>python -m timeit -s "floats = map(float, range(1000))" "ints = [int(x)
> for x in floats]"
> 1000 loops, best of 3: 721 usec per loop
> 
> C:\>python -m timeit -s "floats = map(float, range(1000))" "ints = []"
> "for x in floats: ints.append(int(x))"
> 1000 loops, best of 3: 992 usec per loop
> 
> For builtin functions, map is usually the fastest option (and, IMO, the
> most readable). List comprehensions should be preferred to map + lambda,
> though.
> 

>From the "Evil Coder's Guide to Fast Code":

$ py24 -m timeit -s "floats = map(float, range(1000))" "ints = map(int,
floats)"
1000 loops, best of 3: 442 usec per loop
$ py24 -m timeit -s "floats = map(float, range(1000))" -s"from itertools
import starmap, izip" "ints = list(starmap(int, izip(floats)))"
1000 loops, best of 3: 343 usec per loop

Raymond Hettinger must be doing something smart here that should be ported
to the map() builtin.

Peter




More information about the Python-list mailing list