Speedups In Looping Structures
Solution 1:
First, in python3.x, map
returns an iterable
, NOT a list, so that explains the 50kx speedup there. To make it a fair timing, in python3.x you'd need list(map(...))
.
Second, .append
will be slower because each time through the loop, the interpretter needs to look up the list, then it needs to look up the append
function on the list. This additional .append
lookup does not need to happen with the list-comp or map.
Finally, with the list-comprehension, I believe the function square
needs to be looked up at every turn of your loop. With map, it is only looked up when you call map which is why if you're calling a function in your list-comprehension, map
will typically be faster. Note that a list-comprehension usually beats out map
with a lambda
function though.
Post a Comment for "Speedups In Looping Structures"