简体   繁体   中英

Performance of loops, list comprehensions and maps in Python 3

How is it possible to properly compare performance of for loops, list comprehensions and maps in Python 3.6?

In the code below, plain old for loops perform quite well (I used list() to get values from generators). Am I doing something wrong here? The results are in stark contrast to the discussion on Python 2 .

import timeit

code_for = """
for i in range(1000):
    hex(i)
"""

code_map = """
list(map(hex, range(1000)))
"""

code_map_lambda = """
list(map(lambda x: hex(x), range(1000)))
"""

code_list_comprehension = """
[hex(x) for x in range(1000)]
"""

print(timeit.timeit(code_for, number=10000))
# 1.1155821208376437

print(timeit.timeit(code_map, number=10000))
# 0.8820606248918921

print(timeit.timeit(code_map_lambda, number=10000))
# 1.7510833400301635

print(timeit.timeit(code_list_comprehension, number=10000))
# 1.1798800542019308

UPDATE: Adding elements to a list in code_for

code_for_2 = """
a = [0] * 1000
for i in range(1000):
    a[i] = hex(i)
"""
# 1.243549756007269

code_for_3 = """
a = []
for i in range(1000):
    a.append(hex(i))
"""
# 1.5462996119167656    

A few pointers:

  1. Wrap your code in functions for clarity.
  2. You are missing list creation and appends in code_for . This is the bulk of the cost of using an explicit for loop.
  3. You can then use timeit , or if you have Jupyter notebook, the magic %timeit command.

As below, map without lambda performs best, which makes sense since hex is a built-in. See Python List Comprehension Vs. Map for more details.

def code_for(n):
    res = []
    for i in range(n):
        res.append(hex(i))
    return res

def code_map(n):
    return list(map(hex, range(n)))

def code_map_lambda(n):
    return list(map(lambda x: hex(x), range(n)))

def code_list_comprehension(n):
    return [hex(x) for x in range(n)]

%timeit code_for(10000)                 # 3.19 ms per loop
%timeit code_map(10000)                 # 1.69 ms per loop
%timeit code_map_lambda(10000)          # 3.06 ms per loop
%timeit code_list_comprehension(10000)  # 2.27 ms per loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM