Re: Integer From A Float List?!?

From: Peter Otten (__peter___at_web.de)
Date: 03/05/05


Date: Sat, 05 Mar 2005 08:35:58 +0100

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



Relevant Pages

  • Re: Optimizing multiple dispatch
    ... > I've asked for optimization advice a few times here, ... I wonder where people get this idea that map ... 1000 loops, best of 3: 230 usec per loop ...
    (comp.lang.python)
  • Re: Can I overload the compare (cmp()) function for a Lists ([]) index function?
    ... 100000 loops, best of 3: 5.05 usec per loop ... Functions like filter() and mapare really only more efficient when you have an existing C-coded function, like ``map``. ... Of course, if the filtercode is clearer to you, feel free to use it, but I find that most folks find list comprehensions easier to read than map() and filtercode. ...
    (comp.lang.python)
  • RE: string goes away
    ... > mapat all is for improving the performance. ... Note that you can use `str.upper` in map ... ... 1000 loops, best of 3: 304 usec per loop ...
    (comp.lang.python)
  • Re: string goes away
    ... I read in an guidline for improving Python's performance that you should prefer map() compared to list comprehensions. ... 100000 loops, best of 3: 9.24 usec per loop ...
    (comp.lang.python)
  • Re: Using java.util.map
    ... You perform a lookup and an insertion. ... these operations degenerate each element in the map would have been visited. ... create an extra pairobject for each entry), and the Java way is dirty. ... problems understanding simple for loops of the sort ...
    (comp.lang.java.advocacy)