Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Which is faster, [] or list () of Python knotty problems?

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the "Python problem [] and list () which is fast". In the daily operation, I believe many people have doubts about the Python problem [] and list (). The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "Python problem [] and list ()". Next, please follow the editor to study!

1. [] is three times faster than list ().

For the first question, you can simply measure it using the timeit () function of the timeit module:

> import timeit > timeit.timeit ('[]', number=10**7) > timeit.timeit ('list ()', number=10**7)

As shown in the figure above, with 10 million calls each, the [] creation method takes only 0.47 seconds, while the list () creation method takes 1.75 seconds, so the latter takes 3.7 times as long as the former!

This answers the question: [] is much faster than list () when creating an empty list.

Note: the efficiency of the timeit () function is related to the running environment, and the result of each execution is slightly different. I experimented with the Python3.8 version several times, and overall [] is a little more than three times faster than list ().

2. List () has more steps than []

So let's move on to the second question: why is [] faster?

This time we can use the dis () function of the dis module to see the difference in the bytecode executed by the two:

> from dis import dis > dis ("[]") > dis ("list ()")

As shown in the figure above, the bytecode of [] has two instructions (BUILD_LIST and RETURN_VALUE), while the bytecode of list () has three instructions (LOAD_NAME, CALL_FUNCTION, and RETURN_VALUE).

What do these instructions mean? How to understand it?

First of all, for [], it is a set of literals (literal) in Python, which, like literal quantities such as numbers, represent exact fixed values.

That is, when Python parses it, it knows that it is going to represent a list, so it directly calls the method of building the list in the interpreter (corresponding to BUILD_LIST) to create the list, so it is done in one step.

For list (), "list" is just a common name, not a literal quantity, which means that the interpreter doesn't recognize it at first.

Therefore, the first step of the interpreter is to find the name (corresponding to LOAD_NAME). It looks for each scope one by one in a certain order (local scope-global scope-built-in scope) until it is found, and throws a NameError if it cannot be found.

The interpreter sees "list" followed by a pair of parentheses, so the second step is to call the name as a callable object, that is, as a function (corresponding to CALL_FUNCTION).

Therefore, when list () creates a list, it needs to go through two steps of name lookup and function call before it can really start creating the list. (note: CALL_FUNCTION will also have some function call procedures at the bottom to get to the logic that connects with BUILD_LIST, which we ignore here.)

At this point, we can answer the previous question: because list () involves more execution steps, it is slower than [].

3. Speed up of list ()

After reading the process of answering the first two questions, you may not find it enjoyable enough, and you may think that even if you know this cold knowledge, it will not be of much help, as if the slight improvement seems insignificant.

Because of my habit of divergent thinking, I also came up with another interesting question: can the speed of list () be improved?

In the just released version of Python 3.9.0, it implements a faster vectorcall protocol for list (), so there will be some improvement in execution speed.

At this point, the study of "which is faster of Python [] or list ()" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report