pythonistas: which is faster, where a
is something like [ str(x) for x in list(range(100)) ]
?
ints = map(int, a)
Or
ints = [ int(x) for x in a ]
Assuming a
will be a relatively large list of strings...
pythonistas: which is faster, where a
is something like [ str(x) for x in list(range(100)) ]
?
ints = map(int, a)
Or
ints = [ int(x) for x in a ]
Assuming a
will be a relatively large list of strings...
map
looks faster (without lambda) on my laptop (Macbook Pro Mid 2014, OSX 10.11.4, 16GB DDR3 ram, 2.2 GHz Intel Core i7):
Tested with Python 2.7.10
>>> timeit.timeit("[int(x) for x in range(100)]", number=100000)
1.6301331520080566
>>> timeit.timeit("map(int, range(100))", number=100000)
0.9462239742279053
However, using map with a lambda function is the slowest:
>>> timeit.timeit("map(lambda x: int(x), range(100))", number=100000)
2.285655994415283