Home > Article > Backend Development > Why Are Python List Comprehensions So Much Faster Than Appending to Lists?
Python programmers often notice a significant performance gap between list comprehensions and appending to a list. While list comprehensions are commonly perceived as a syntactic convenience, they demonstrate superior speed that extends beyond mere expressiveness.
To illustrate this phenomenon, consider the following timeit measurements:
>>> import timeit >>> timeit.timeit(stmt=''' t = [] for i in range(10000): t.append(i)''', number=10000) 9.467898777974142 >>> timeit.timeit(stmt='t= [i for i in range(10000)]', number=10000) 4.1138417314859
As evident from the measurements, list comprehension is nearly 50% faster than appending. Let's delve into the underlying reasons.
List comprehensions are not mere syntactic sugar for regular for loops. They avoid the overhead associated with accessing and invoking the append attribute in every iteration. This eliminates multiple function frame suspension and resumption, which is inherently slower than constructing a list directly.
Examining the disassembled bytecodes for the two approaches sheds light on their performance disparity:
# Appending to a List 18 LOAD_FAST 0 (l) 20 LOAD_METHOD 1 (append) 22 LOAD_FAST 1 (i) 24 CALL_METHOD 1 26 POP_TOP # List Comprehension 2 BUILD_LIST 0 8 LOAD_FAST 1 (i) 10 LIST_APPEND 2
The append method invocation in the first function incurs additional overhead, while the list comprehension directly extends the newly created list.
In scenarios where performance is paramount, list comprehensions should be the preferred approach over appending to a list. Their superior speed stems from their ability to bypass extraneous bytecodes and the function calling overhead.
The above is the detailed content of Why Are Python List Comprehensions So Much Faster Than Appending to Lists?. For more information, please follow other related articles on the PHP Chinese website!