python多处理(ThreadPool)执行时间比顺序更长

时间:2018-05-02 13:31:02

标签: python multithreading multiprocessing threadpool

我是python中multiprocessing模块的新手。我在下面有一个测试代码,用于计算以顺序形式和线程池执行函数所需的时间:\

from multiprocessing.dummy import Pool as ThreadPool
import timeit


def foo(n):

    for i in range(700000):
        n += 1
    return n


inputs = [1,2, 3, 4]

start = timeit.default_timer()

pool = ThreadPool(4)

results = pool.map(foo, inputs)

pool.close()
pool.join()

print(timeit.default_timer() - start)
print(results)

start = timeit.default_timer()

for i in range(4):
    foo(i+1)

print(timeit.default_timer() - start)

这是我不断得到的结果:

0.3945475000073202
[700001, 700002, 700003, 700004]
0.2912912300089374

Process finished with exit code 0

顺序算法比线程算法更快?我知道在创建线程时会产生开销,但我做了foo这样就需要花费大量的时间来计算。多线程使用4个线程需要(大约)顺序算法的四分之一时间吗? 对我来说这真的很奇怪,因为当我测试下面的算法时,foo函数的差异基本相同,我在速度方面得到了更好的结果:

def foo(n):  
   time.sleep(n)


inputs = [1, 1, 1, 1]
start = timeit.default_timer()

pool = ThreadPool(4)

results = pool.map(foo, inputs)

pool.close()
pool.join()

print("Threaded - " + str(timeit.default_timer() - start))

start = timeit.default_timer()

for i in range(4):
    foo(1)

print("sequential - " + str(timeit.default_timer() - start))

使用此代码,多线程似乎工作正常:

Threaded - 1.2794823400327004
sequential - 4.011253927950747

Process finished with exit code 0

0 个答案:

没有答案