Python 多处理似乎比常规执行慢 [英] Python multiprocessing seems slower than regular execution
问题描述
在下面的代码中,我正在生成数字为 9999 的立方体并通过线程池和普通方法调用它.
In the code below, I am generating cube of a number 9999 and calling the same via thread pool and normal method.
我正在计算两者之间的差异.似乎正常方法要快得多.我正在 i7 第 8 代英特尔处理器上运行它,在 python 2.7 终端内有 16 gig ram.
I am timing the difference between the same. Seems like the normal method is way faster. I am running this on a i7 8th gen intel processor with 16 gig ram inside a python 2.7 terminal.
我对此感到困惑.可能是我错过了什么.我希望这个问题对未来的人有所帮助.
I am baffled by this. May be I am missing something. I hope this question is helpful for people in the future.
import time
from multiprocessing.pool import ThreadPool
def cube():
return 9999*9999*9999
print "Start Execution Threading: "
x = int(round(time.time() * 1000))
pool = ThreadPool()
for i in range(0,100):
result = pool.apply_async(cube, ())
result = pool.apply_async(cube, ())
result = pool.apply_async(cube, ())
# print result.get()
pool.close()
pool.join()
print "Stop Execution Threading: "
y = int(round(time.time() * 1000))
print y-x
print "Start Execution Main: "
x = int(round(time.time() * 1000))
for i in range(0,100):
cube()
cube()
cube()
print "Stop Execution Main: "
y = int(round(time.time() * 1000))
print y-x
推荐答案
多处理意味着您将开始一个新线程.这带来了相当大的开销,因为它必须被初始化.因此,多线程只会带来回报,尤其是在 Python 中,当您并行化所有独立执行需要大量时间(与 Python 启动时间相比)并且可以允许异步运行的任务时.
Multiprocessing means you will start a new thread. That comes with quite an overhead in that it must be initialized. As such, multi-threading only pays off, especially in python, when you parallelize tasks which all on their own take considerable time to execute (in comparison to python start-up time) and which can be allowed to run asyncronously.
在你的例子中,一个简单的乘法执行得如此之快,它不会有回报.
In your case, a simple multiplication, is so quickly executed it will not pay off.
这篇关于Python 多处理似乎比常规执行慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!