map, LC, genexp

Robert Kern rkern at ucsd.edu
Sat Nov 13 23:19:33 EST 2004


Bryan wrote:
> i thought that LC and genexp were supposed to be faster than map.  i 
> also thought i read somewhere in this group that the slowest looping 
> mechanism in 2.4 is 20% faster than the fastest looping mechanism in 
> 2.3.  if i'm wrong about this, remembered wrong or did something wrong, 
> please let me know.  here are some timeit tests i did on my machine.  
> notice that map is faster in both python versions and that LC24/LC23 is 
> only a 5.5% speed improvement.
> 
> python 2.3
> 
> timeit "for i in map(str, xrange(100)): pass"
> 10000 loops, best of 3: 118 usec per loop

map still beats list comprehensions and generator expressions when the 
function is builtin. Try again with a Python function that gets 
"inlined" in the LC and genexp:

E.g.

def neg(x):
     return -x

map(neg, xrange(100))
[-x for x in xrange(100)]
(-x for x in xrange(100))

I believe (though I haven't tested this specific example) that is the 
case for which they are faster than map.

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter



More information about the Python-list mailing list