In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to improve the performance of the Python program, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Python is not good at performance, but it can also improve program performance as much as possible and avoid unnecessary waste of resources by mastering some skills.
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, such as ls=os.linesep, with local variables. 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.
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 fromXimportY instead of importX;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).
3. Notice of function
1. Does the function definition start with def?
two。 Is the function name made up of characters and underscores _?
3. Is the function name followed by parentheses (?
4. Are parameters included in parentheses? Are multiple parameters separated by commas?
5. Is the parameter name duplicated? (duplicate parameter names cannot be used)
6. Are parameters followed by parentheses and colons):?
7. Does the code that follows the function definition use 4-space indent?
8. Is indentation canceled at the end of the function ("dedent")?
When you run (or "use use" or "call call") a function, remember to check the following points:
1. Do you use the name of the function when invoking the function?
two。 Is the function name followed by (?
3. Are there any parameters after parentheses? Are multiple parameters separated by commas?
4. Does the function end with)?
Check your exercises according to the contents of these two checklists until you don't need to check them.
Finally, read the following sentence several times:
"'run', 'call' and 'use' have the same meaning."
4. Use mapping alternative condition 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.
5. Direct iterative sequence elements
For sequences (str, list, tuple, etc.), directly iterating sequence elements is faster than iterative elements.
6. Use generator expression instead of list parsing
List parsing (listcomprehension) 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.
7. Compile before calling
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.
8. 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 the value is x27; (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.
Thank you for reading this article carefully. I hope the article "how to improve the performance of Python" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.