For CPU-intensive programs, you can use packaged classes such as Multiprocessing's process,pool to implement parallel computations in a multi-process manner. However, because the communication cost in the process is relatively large, the efficiency of the program that requires a lot of data interaction between processes may not be greatly improved.
4, for the optimization of the cycle
Each programming language will emphasize the need to optimize loops. When using Python, you can rely on a lot of tricks to make loops run faster. However, one way developers often miss out is to avoid using point operations in a loop. For example, consider the following code:
Every time you call a method, Str.upper,python will ask for the value of the method. However, if you use a variable instead of the evaluated value, the value becomes known, and Python can perform the task more quickly. The key to optimizing the loop is to reduce the amount of work that Python performs inside the loop, because the Python native interpreter really slows down execution in that case.
In addition to the Timeit modules used above in Ipython, there are cprofile. CProfile is also very simple to use: Python–m cProfile filename.py,filename.py is the file name to run the program, you can see in the standard output the number of times each function was called and the time it ran, to find the program's performance bottleneck, and then to be targeted for optimization.
Incoming group: 125240963 to get dozens of sets of PDFs Oh!
Send you six efficiency artifacts that run Python! It's too strong to be strong!