I am currently learning Python 3 and thought some speed comparison could be neat. Thus, I created some in-place and some temporary list functions, which simply add 1 to a long list of integers.
However, the results were really surprising to me...it seems that Python 3 is slower in every use case: different tested for loops, a while loop, the map function, and list comprehensions.
Of course (see the code below) it just comparison of in-place list mutations or loops that build up and return a temporary list...so e.g. the "standard" map of Python 3, that just creates an iterator, is still fast, but at some point one would always like to print, write or somehow visualize the data. Hence, something like list(map())
should be relatively common in Python 3?!
What am I missing here?
Is Python 3 really slower than Python 2 for loops, comprehensions and map? If so, why?
Python 3.4 timings:
> python3.4 func_speed_tests.py
list_comprehension.........1.2785s
for_loop...................1.9988s
for_loop_append............1.8471s
for_loop_range.............1.7585s
while_loop.................2.2539s
calc_map...................2.0763s
Python 2.7 timings:
> python2.7 func_speed_tests.py
list_comprehension.........0.9472s
for_loop...................1.2821s
for_loop_append............1.5663s
for_loop_range.............1.3101s
while_loop.................2.1914s
calc_map...................1.9101s
Here is the code:
import timeit
from random import shuffle
# generate a long list of integers
mlst = list(range(0, 1000000))
# randomly sort the list
shuffle(mlst)
# number of benchmark passes
n = 10
#####################
# functions to time #
#####################
def list_comprehension(lst):
return [x + 1 for x in lst]
def for_loop(lst):
for k, v in enumerate(lst):
lst[k] = v + 1
def for_loop_append(lst):
tmp = []
for item in lst:
tmp.append(item + 1)
def for_loop_range(lst):
for k in range(len(lst)):
lst[k] += 1
return lst
def while_loop(lst):
it = iter(lst)
tmp = []
while it:
try:
tmp.append(next(it)+1)
except StopIteration:
break
return tmp
def calc_map(lst):
lst = map(lambda x: x + 1, lst)
return list(lst)
############################################
# helper functions for timing and printing #
############################################
def print_time(func_name):
print("{:.<25}{:.>8.4f}s".format(func_name, timer(func_name)))
def timer(func):
return timeit.timeit(stmt="{}(mlst)".format(func),
setup="from __main__ import mlst, {}".format(func),
number=n)
#################
# run the tests #
#################
print_time("list_comprehension")
print_time("for_loop")
print_time("for_loop_append")
print_time("for_loop_range")
print_time("while_loop")
print_time("calc_map")