When reading articles about the speed of Loop vs List comprehension vs Map, I usually find that list comprehension if faster than map when using lambda functions.
Here is a test I am running:
import timeit
def square(range):
squares = []
for number in range:
squares.append(number*number)
return squares
print(timeit.timeit('map(lambda a: a*a, range(100))', number = 100000))
print(timeit.timeit('[a*a for a in range(100)]', number = 100000))
print(timeit.timeit('square(range(100))', 'from __main__ import square', number = 100000))
and the results :
0.03845796199857432
0.5889980600004492
0.9229458660011005
so Map is the clear winner altough using a lambda function. Has there been a change in python 3.7 causing this notable speed boost ?
map…