In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to carry out in-depth performance analysis of python code Dis module, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.
When the Python code is executed, it is compiled into Python bytecode, and then the Python virtual machine executes the Python bytecode. Sometimes when we execute a python file, we generate a pyc file, which is used to store Python bytecode instructions, which are an intermediate language similar to assembly instructions, but each bytecode corresponds not to a machine instruction, but to a piece of C code.
The Dis module is used to view the running trajectory of these bytecodes, so we can use the Dis module to determine which of the two functions will take up more memory and who will consume more CPU performance. Not only that, through instructions, we can also know some built-in functions in Python, the value process of variables, and running logic, which is very helpful for us to optimize the code.
Here are two examples to introduce the use of the Dis module.
1. Why does the first function consume less memory than the second function?
It is difficult for ordinary people to see directly, but we can easily find the answer using the Dis module:
Results:
The results of Dis are actually easy to read:
First column: the corresponding number of lines of source code.
The second column: the index location of the corresponding memory bytecode.
The > > sign between the first column and the second column indicates the target of the jump
The third column: the operation of internal machine code.
The fourth column: instruction parameters.
The fifth column: actual parameters.
The dis analysis of the two functions is separated by a * sign, and you can clearly see the difference between the statements between the two functions. The bytecode index of the second function is up to 30, while the bytecode index of the first function is only 22, so the first function consumes less memory than the second function.
Also, the > > sign between the first column and the second column indicates the target of the jump. You can look at 18 in the fourth column of the second function, indicating that it jumps to the instruction with index 18, that is, ROT_TWO. The second function also jumps more than the first function, which may also cause it to be less efficient than the first function in some special cases.
two。 Why is while True slower than while 1 in Python2?
While 1: pass while True: pass
You can analyze by using dis in the command:
As you can see, while 1 is a direct JUMP_ABSOLUTE on the second line, so there is less LOAD_NAME and POP_JUMP_IF_FALSE than While True. This is because True is not a keyword in Python2, but a built-in variable, so every time Python uses LOAD_NAME to POP_JUMP_IF_FALSE the value of True. This is why While True is slower than while 1.
When Python3,True becomes a keyword, there is no such problem:
Python 3 has made a lot of replacements for Python 2, which is one of the reasons why it is not compatible with Python 2. There is a big difference. Therefore, it is recommended that beginners learn directly from Python 3 instead of Python 2.
On how to conduct in-depth performance analysis of python code Dis module to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.