14
votes

Referring to this Python List Comprehension Vs. Map question, can someone explain why List Comprehensions gives better results over map when list comprehension does not call a function, even when there is no lambda function in the map but gives the worst result when calling a function?

import timeit

print timeit.Timer('''[i**2 for i in xrange(100)]''').timeit(number = 100000)

print timeit.Timer('''map(lambda i: i**2, xrange(100))''').timeit(number = 100000)

print timeit.Timer(setup="""def my_pow(i):
    return i**2
""",stmt="""map(my_pow, xrange(100))""").timeit(number = 100000)

print timeit.Timer(setup="""def my_pow(i):
    return i**2
""",stmt='''[my_pow(i) for i in xrange(100)]''').timeit(number = 100000)

results:

1.03697046805 <-- list comprehension without function call
1.96599485313 <-- map with lambda function
1.92951520483 <-- map with function call
2.23419570042 <-- list comprehension with function call
1
It doesn't matter whether the function called in map is a lambda or a regular function, the overhead is still there. No idea why a list comprehension with a function call would be slower than map() though. - millimoose
@millimoose but the lambda function gets declared for each itetaration, does this make any change? - zenpoy
@zenpoy: Function call arguments are evaluated before the function is called, so the function is declared only once. - Sven Marnach
cannot delete comments on my phine :( - Joel Cornett
@SvenMarnach i think he/she's talking about my_pow definition being interpreted only once for the whole timeit execution (in setup) and lambda being defined for each iteration. It's a valid question, and lambda probably contributes to it's version being slightly slower. - soulcheck

1 Answers

15
votes

All your timing results can be explained by theses facts:

  1. CPython has a rather high function call overhead.

  2. map(f, it) is slightly faster than [f(x) for x in it].

The first version of your code does not define a function at all, so there is no function call overhead. The second version needs to define a function, ,so there is function call overhead in every iteration. The third version is completely equivalent to the second one – functions and lambda expressions are the same thing. And the last version is even slower according to fact 2.