Python 2.0b1 List comprehensions are slow

Dan Schmidt dfan at thecia.net
Mon Sep 11 08:22:33 EDT 2000


"Alex Martelli" <aleaxit at yahoo.com> writes:

| Consider a few 'equivalent' definitions, such as:
| 
| >>> def withlambda(n):
|  return map(lambda x: x*x, range(n))
| >>> def withcompre(n):
|  return [x*x for x in range(n)]
| >>> def withlocfun(n):
|  def square(x):
|   return x*x
|  return map(square, range(n))
| >>> def square(x):
|  return x*x
| >>> def withglofun(n):
|  return map(square, range(n))
| 
| Performance appears to be roughly like (best-out-of-3 for each):
| >>> profile.run('junk=withlambda(10000)')
|          10003 function calls in 0.886 CPU seconds
| >>> profile.run('junk=withcompre(10000)')
|          3 function calls in 0.063 CPU seconds
| >>> profile.run('junk=withlocfun(10000)')
|          10003 function calls in 0.883 CPU seconds
| >>> profile.run('junk=withglofun(10000)')
|          10003 function calls in 0.877 CPU seconds
| 
| So, there appears to me to be no significant difference between
| the various map versions (lambda, local function, global function),
| while the list-comprehension is over an order of magnitude faster
| by avoiding the function-calls that map always needs.

I don't know a lot about this stuff, but I think that the profiler
isn't adequately compensating for the extra work that it has to do to
track function calls, so the non-comprehension versions get an unfair
handicap.  When I just use wall time like this:

  c = time.clock()
  junk = withlambda (10000)
  delta = time.clock() - c
  print "withlambda: %.3f seconds" % delta

I get the following (pretty consistent) results:

  withlambda: 0.029 seconds
  withcompre: 0.054 seconds
  withlocfun: 0.031 seconds
  withglofun: 0.033 seconds

which seem more consistent with the list comprehension performance
that other people have been reporting.

-- 
                 Dan Schmidt | http://www.dfan.org
Honest Bob CD now available! | http://www.dfan.org/honestbob/cd.html



More information about the Python-list mailing list