Ignore the answer of everyone else for now. The first thing you should learn to use is a profiler. Python comes with the / cProfile profile; You must learn to read the results and analyze where the real bottlenecks are. The optimization task consists of three parts: reduce the time spent on each call, reduce the number of calls made and reduce memory usage to reduce disk overload.
The first goal is relatively simple. The profiler will show you the most time-consuming functions, and you can immediately go to this function to optimize it.
The second and third goal is more difficult, because it means that you need to change the algorithm to reduce the need to make so many calls. Find functions that have a large number of calls, and try to find ways to reduce the need for their calls. Use the built-in collections, they are very well optimized.
If you do a lot of processing numbers and arrays, you should take a look at pandas, Numpy / Scipy, gmpy third-party modules; they are well optimized by C libraries for processing arrays / tabular data.
Another thing you want to try is PyPy. PyPy can JIT recompile and perform much more complex optimizations than CPython, and it will work without the need to change Python code. Although well-optimized CPython-oriented code can be very different from well-optimized PyPy-oriented code.
The next one to try is Cython. Cython is a slightly different language than Python, in fact Cython is best described as C with typed Python-like syntax.
For parts of your code that are in very narrow loops that you can no longer optimize using any other methods, you can rewrite it as a C. extension. Python has very good support for an extension with C. In PyPy, the best way to extend PyPy is cffi.
Lie ryan
source share