Language Shootout
Andrew Gaul
gaul at spam.utexas.edu
Wed Jul 11 19:30:21 EDT 2001
In article <mailman.994689198.3317.python-list at python.org>, Roman Suzi wrote:
> One more point about shootout. If I understood correctly, advanced
> features are banned. You can't change
>
> k = []
> for i in list:
> k.append(f(i)
>
> into:
>
> k = [f(i) for i in list]
Actually, the former isn't much slower than the latter. Using map is
almost twice as fast. I believe that map generates the result list by
calling len() on the source list, avoiding the cost of growing the
result list. I suppose list comprehensions could be implemented such
that if there were not a filter clause, to prealloacte the list. But
then, one should be using map anyway (you're only saving an implicit
lambda using a list comprehension).
meatring [~]$ time python2.1 -c 'k = []
for x in xrange(1000000):
k.append(x)'
real 0m3.934s
user 0m3.440s
sys 0m0.490s
meatring [~]$ time python2.1 -c '[ x+1 for x in xrange(1000000) ]'
real 0m3.402s
user 0m2.670s
sys 0m0.720s
meatring [~]$ time python2.1 -c 'map(lambda x: x+1, xrange(1000000))'
real 0m1.899s
user 0m1.840s
sys 0m0.060s
--
| a | n | d | r | e | w | @ | g | a | u | l | . | o | r | g |
White trash. Loser. Geek.
More information about the Python-list
mailing list