Send you six efficiency artifacts that run Python! It's too strong to be strong!

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.