Re: Integer From A Float List?!?
From: Peter Otten (__peter___at_web.de)
Date: 03/05/05
- Next message: QMartin_v=2E_L=F6wis=22?=: "Re: locale support and 4.10"
- Previous message: devendra_k_at_citilindia.com: "intigrate the PyGame module with my Python"
- In reply to: Nick Coghlan: "Re: Integer From A Float List?!?"
- Next in thread: Michael Hoffman: "Re: Integer From A Float List?!?"
- Reply: Michael Hoffman: "Re: Integer From A Float List?!?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: QMartin_v=2E_L=F6wis=22?=: "Re: locale support and 4.10"
- Previous message: devendra_k_at_citilindia.com: "intigrate the PyGame module with my Python"
- In reply to: Nick Coghlan: "Re: Integer From A Float List?!?"
- Next in thread: Michael Hoffman: "Re: Integer From A Float List?!?"
- Reply: Michael Hoffman: "Re: Integer From A Float List?!?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|