Table of Contents
回复内容:
Home Backend Development Python Tutorial 为什么Python程序不怎么占用CPU资源?

为什么Python程序不怎么占用CPU资源?

Jun 06, 2016 pm 04:23 PM
cpu cython python

只是个人感觉,写一些computationally intense的代码,C++的版本很快,而Python的版本会慢得多。但是比较奇怪的一点在于,Python版本一般不会占用全部CPU资源,而C++的就是100%,有没有什么办法让Python code更有效地调用CPU资源?(在Python框架内,不考虑Cython或者Python-C API)

回复内容:

正确的解决方法就是,纯计算的问题就应该用C++来写,然后你好好学习如何将任意两种编程语言互相打通,以后就再也不会思考这个无聊的问题了。 这是我用 matplotlib 画最近全部股票走势时候的任务截图。

为什么Python程序不怎么占用CPU资源? python要想占满cpu,不用c扩展的话,就只能搞多进程了。对于需要线程间大量交换数据、适合共享内存的那种计算密集型问题。。。还是放弃python比较明智。当然,很多data-parallel型的数值计算问题,没必要放弃python写c扩展,可以考虑numba/theano/numexpr等优化工具。 看症状,题主最大的可能是C++版本可能在内部某处调用了多线程,Python在有GIL的情况下,最多也就只能占满一核,所以才会有怎么跑不满的疑问

上面的答案都说的很好了,要占满内核请上cython,捎上nogil:
Search Results · GitHub Python本来就是用来做Casual Work的。要不是大多数CLI Shell的编程语法不够现代,Python还不如那些Shell呢(人家好歹不会搞个版本大分裂) 我一不小心看成了程序员怎么不吃cpu_(:_」∠)_对不起问主 C++占用了100%CPU,你确定程序写对了? 多用numpy… python有一个全局锁,一个pyhton程序无论何时只能占用一个cpu,当然,在这个cpu上,你是可以跑满100%的,对于现在的多cpu系统,想要充分利用cpu资源,你只能选择使用多进程技术(多线程也不行哦)。提速的方法有很多,比如编写c扩张,使用pypy编译器,还有优化代码结构等等。 为什么不用multiprocessing么?
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

144-core, 3D-stacked SRAM: Fujitsu details next-generation data center processor MONAKA 144-core, 3D-stacked SRAM: Fujitsu details next-generation data center processor MONAKA Jul 29, 2024 am 11:40 AM

144-core, 3D-stacked SRAM: Fujitsu details next-generation data center processor MONAKA

Leak reveals key specs of Intel Arrow Lake-U, -H, -HX and -S Leak reveals key specs of Intel Arrow Lake-U, -H, -HX and -S Jun 15, 2024 pm 09:49 PM

Leak reveals key specs of Intel Arrow Lake-U, -H, -HX and -S

AM4 refuses to die, news says AMD will launch Ryzen 9 5900XT/7 5800XT: clocked at up to 4.8GHz AM4 refuses to die, news says AMD will launch Ryzen 9 5900XT/7 5800XT: clocked at up to 4.8GHz Jun 05, 2024 pm 09:43 PM

AM4 refuses to die, news says AMD will launch Ryzen 9 5900XT/7 5800XT: clocked at up to 4.8GHz

How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

How to download deepseek Xiaomi

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

Intel CPU size and core scheduling issues, CPU size and core optimization settings above 12 generations Intel CPU size and core scheduling issues, CPU size and core optimization settings above 12 generations Jun 19, 2024 am 01:42 AM

Intel CPU size and core scheduling issues, CPU size and core optimization settings above 12 generations

Solution to Intel 13th and 14th generation processor stability issues Solution to Intel 13th and 14th generation processor stability issues Jun 18, 2024 pm 06:01 PM

Solution to Intel 13th and 14th generation processor stability issues

How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

How do you ask him deepseek

See all articles