In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to improve the performance of Python", the content is easy to understand, clear, hope to help you solve your doubts, let the editor lead you to study and learn "how to improve the performance of Python" this article.
Tip 1. Use local variables
Try to use local variables instead of global variables: easy to maintain, improve performance, and save memory.
Replace variables in the module namespace with local variables, such as? ls?=?os.linesep. On the one hand, it can improve the program performance and find local variables faster; on the other hand, short identifiers can be used to replace lengthy module variables to improve readability.
Tip 2. Reduce the number of function calls
When judging the object type, isinstance () is the best, the object type identity (id ()) is the second, and the object value (type ()) is the best.
Do not put the content of the repeated operation as a parameter in the loop condition to avoid repeated operation.
If you want to use a function or object Y in module X, you should directly use from?X?import?Y instead of import?X;?X.Y. This reduces one query when using Y (the interpreter does not have to find the X module first and then look for Y in the dictionary of the X module).
Technique 3. Use mapping instead of conditions to find
Maps (such as dict, etc.) search much faster than conditional statements (such as if, etc.). There is also no select-case statement in Python.
Technique 4. Direct iterative sequence elements
For sequences (str, list, tuple, etc.), directly iterating sequence elements is faster than iterative elements.
Tip 5. Using generator expressions instead of list parsing
List parsing (list?comprehension) produces the whole list, and iterating over a large amount of data has a negative effect.
The generator expression, on the other hand, does not actually create a list, but returns a generator that produces a value (deferred evaluation) when needed, which is more memory-friendly.
Tip 6. Compile first and then call
When using eval () and exec () functions to execute code, it is best to call the code object (compiled into bytecode through the compile () function in advance) instead of calling str directly, which can avoid repeated compilation and improve program performance.
Regular expression pattern matching is similar, and it is also best to compile the regular expression pattern into a regex object (through the re.complie () function) before performing a comparison and match.
Tips 7. Module programming habits
The highest-level Python statements in the module (no indented code) are executed when the module is imported (import) (whether it is really necessary or not). Therefore, we should try to put all the functional code of the module into the function, including the functional code related to the main program can also be put into the main () function, and the main program itself calls the main () function.
You can write test code in the module's main () function. In the main program, the value of name is detected, and if it is' main' (indicating that the module is executed directly), the main () function is called to test; if it is the name of the module (indicating that the module is called), the test is not performed.
The above is all the contents of the article "how to improve the performance of Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.