Python: List Comprehensions vs. map

后端 未结 1 862
北恋
北恋 2020-12-31 09:03

Referring to this Python List Comprehension Vs. Map question, can someone explain why List Comprehensions gives better results over map when list comprehension

相关标签:
1条回答
  • 2020-12-31 09:17

    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.

    0 讨论(0)
提交回复
热议问题