Why do Python programs not occupy much CPU resources?

Source: Internet
Author: User
I personally feel that writing computationallyintense code is very fast in C ++, while Python is much slower. However, the strange thing is that Python generally does not occupy all CPU resources, and C ++ is 100%. Is there any way for Pythoncode to call CPU resources more effectively? (In the Python framework, Cython or Python-CAPI is not considered.) In my personal opinion, writing computationally intense code is very fast, while the Python version is much slower. However, the strange thing is that the Python version generally does not occupy all CPU resources, and the C ++ version is 100%. Is there any way for the Python code to call CPU resources more effectively? (In the Python framework, Cython or Python-c api is not taken into account.) reply content: the correct solution is to use C ++ for pure computing, then you have learned how to connect any two programming languages, and you will never think about this boring question again. This is my task to use matplotlib to draw all recent stock movements.

To fully occupy the cpu, python can only implement multiple processes without using c extension. For Computing-intensive issues that require mass data exchange between threads and are suitable for Memory sharing... It is wise to give up python. Of course, there are a lot of data-parallel-type numerical computing problems. You don't have to give up writing c extensions in python. You can consider numba, theano, numexpr, and other optimization tools. Looking at the symptoms, the biggest possible cause for the subject is that the C ++ version may call multiple threads somewhere inside. In the case of GIL, Python can only occupy one core at most, so there will be questions about how to run the game.

The above answers are all very good. To fully occupy the kernel, please use cython and add nogil:
Search Results GitHub Python is used for Casual Work. If most of the CLI Shell programming syntax is not modern enough, Python is not as good as those shells (people may not make a major version split). I accidentally think that programmers do not eat cpu _(: _ "memory) _ Sorry, the main C ++ occupies 100% of the CPU. Are you sure the program is correct? Use numpy more... Python has a global lock. A pyhton program can only occupy one cpu at any time. Of course, on this cpu, you can run 100% full. For the current multi-cpu system, to make full use of cpu resources, you can only choose to use multi-process technology (multithreading is not acceptable ). There are many methods to speed up, such as writing c extensions, using the pypy compiler, and optimizing the code structure. Why not use multiprocessing?

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.