Why is Appending to a List Significantly Slower than List Comprehension?
List comprehension has gained popularity in Python due to its brevity and efficiency. While it may appear as a syntactic shortcut for a regular for loop, it offers significant performance advantages, especially when appending elements to a list.
Benchmarking the Difference
Consider the following code snippet:
import timeit timeit.timeit(stmt=''' t = [] for i in range(10000): t.append(i)''', number=10000) timeit.timeit(stmt='t= [i for i in range(10000)]', number=10000)
As evident from the results, list comprehension is substantially faster, outperforming the appending approach by approximately 50%.
Delving into the Reasons
List comprehension is essentially a syntactic construct that generates a new list based on an existing iterable. Unlike the appending method, it doesn't require the retrieval and invocation of the append attribute at each iteration.
Code Disassembly
A deeper analysis using the disassembler provides insights into the underlying differences:
# Function using appending dis.dis(f1)
In the disassembled code for the function using appending, there's a noticeable LOAD_METHOD and CALL_METHOD pair for each iteration (bytecodes 18-22). These instructions handle the loading and invocation of the append attribute, which incurs an overhead.
# Function using list comprehension dis.dis(f2)
In contrast, the list comprehension version (bytecodes 10-12) involves a single CALL_FUNCTION instruction. This instruction efficiently constructs a new list without the need for attribute retrieval.
Conclusion
The improved efficiency of list comprehension stems from its optimized implementation. By avoiding the repeated loading and invocation of the append attribute, list comprehension creates lists on demand, resulting in significant performance gains, particularly when working with large iterations.
The above is the detailed content of Why is List Comprehension Significantly Faster Than Appending to a List in Python?. For more information, please follow other related articles on the PHP Chinese website!