Python is not well-known for performance, but mastering some skills can also maximize program performance and avoid unnecessary waste of resources.
1. Using Local variables
Try to use local variables instead of global variables: Ease of maintenance, improved performance, and memory savings.
Use local variables to replace variables in the module namespace, such as ls = Os.linesep. On the one hand, it can improve the performance of the program, local variable find faster, on the other hand, a short identifier can replace the lengthy module variable, improve readability.
2. Reduce the number of function calls
When the object type is judged, the isinstance () is optimal, followed by the object type identity (ID ()), and the object value (type ()) is used to compare the most time.
Avoid repeating operations by placing the contents of the repeating operation in the loop condition as a parameter.
If you want to use a function or object Y in module X, you should use from X import y instead of import x directly; X.y. This allows you to reduce the query once you use Y (the interpreter does not have to find the X module first and then finds y in the dictionary of the X module).
3, using the mapping alternative conditions to find
Mappings (such as dict, etc.) are searched much faster than conditional statements (such as if, etc.). There are no select-case statements in Python.
4. Direct Iterative sequence elements
For sequences (str, list, tuple, and so on), the direct iteration of sequence elements is faster than the index of iterative elements.
5. Using Generator expression Substitution list parsing
List comprehension, which produces an entire list, has a negative effect on iterations of large amounts of data.
The generator expression does not, it does not actually create the list, but instead returns a generator that produces a value (deferred calculation) when needed and is more friendly to memory.
6, first compiled after the call
When executing code using the eval (), exec () function, it is best to call the Code object (which is compiled into bytecode in advance via the compile () function) instead of directly calling STR, which avoids repeating the compilation process multiple times and improves program performance.
Regular expression pattern matching is also similar, and it is best to compile the regular expression pattern into a Regex object (via the Re.complie () function) before performing a comparison and matching.
7. Module Programming Habits
The highest level Python statement in the module (code without indentation) executes when the module is imported (import), regardless of whether it is really necessary to execute. Therefore, you should try to put all the function code of the module into the function, including the main program related function code can also be placed in the main () function, the main program itself calls the main () function.
You can write the test code in the main () function of the module. In the main program, the value of the __name__ is detected, and if ' __main__ ' (indicating that the module is executed directly), the main () function is called, and if the module name (which indicates that the module is called) is not tested.
Develop these 7 good programming habits that can greatly increase your Python performance