Why are Python programs not taking up CPU resources?

Source: Internet
Author: User
Just a personal feeling, write some computationally intense code, C + + version is fast, and Python version is much slower. But the strange point is that the Python version generally does not occupy all the CPU resources, and C + + is 100%, is there any way to make Python code more efficient call CPU resources? (within the Python framework, the Cython or python-c API is not considered)

Reply content:

The correct solution is that the problem of pure computing should be written in C + +, and then you learn how to connect any two programming languages to each other, and then never think about this boring problem. This is my task when I use Matplotlib to draw the most recent stock trend.

Python can only get a lot of progress if it wants to fill up the CPU without the C extension. A computationally intensive problem that requires a large amount of data exchange between threads, suitable for shared memory ... It's wiser to abandon Python. Of course, many data-parallel types of numerical computing problems, there is no need to abandon the Python write C extension, you can consider the numba/theano/numexpr and other optimization tools. Look at the symptoms, the main problem is that the C + + version may be in the internal call a multi-threaded, python in the case of the Gil, at most can only occupy one core, so there will be how to run the question of dissatisfaction

The above answer is very good, to fill the kernel please Cython, take on the Nogil:
Search Results GitHub Python is supposed to be used for casual work. If not most of the CLI shell programming syntax is not modern enough, Python is not as good as those shells (people will not make a version of the big split) I accidentally see the programmer how not to eat cpu_ (: _"∠) _ Sorry to ask the main C + + occupied 100%cpu, you sure the program is written right? Multi-use Numpy...python has a global lock, a Pyhton program can only occupy a CPU at any time, of course, on this CPU, you can run full 100%, for the current multi-CPU system, want to make full use of CPU resources, you can only choose to use multi-process technology ( Multithreading is not good, too.) There are many ways to speed up, such as writing C expansion, using the PyPy compiler, optimizing the code structure, and so on. Why don't you multiprocessing?
  • 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.