Why is List Comprehension Significantly Faster Than Appending to a List in Python?

Patricia Arquette
Release: 2024-10-28 03:47:02
Original
260 people have browsed it

Why is List Comprehension Significantly Faster Than Appending to a List in Python?

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)
Copy after login

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)
Copy after login

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)
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!