Python - 多处理比顺序慢?

时间:2016-11-02 19:43:56

标签: python multiprocessing

我刚刚进行多处理(与线程相反,这应该是利用可用的多个核心)。我使用一些虚拟计算来测试速度:

def g(number):
    f = (math.factorial(number))
    f = f * 10

然后我尝试在测量时间的同时顺序调用该函数:

start_time = time.time()  
g(10000)
g(2000)
g(3000)
print("--- %s seconds ---" % (time.time() - start_time))

这会返回--- 0.003850698471069336 seconds --- 现在,当我通过

尝试多处理时
start_time_ = time.time()
p = Pool(3)
p.map(g, [10000,2000,3000])
print("--- %s seconds ---" % (time.time() - start_time_))

start_time_1 = time.time()
pool = Pool(processes=3)
pool.map(g, [10000,2000,3000])
print("--- %s seconds ---" % (time.time() - start_time_1))

我的运行时间更短,--- 0.0334630012512207 seconds ------ 0.6992270946502686 seconds ---

为什么在3个不同的核心/进程上运行3次计算会导致更大的计算时间?难道我做错了什么? 谢谢!

0 个答案:

没有答案