为什么并发期货比我的循环慢?

时间:2018-12-24 22:40:09

标签: python concurrent.futures

大家好,我想通过使用并发期货来加快多处理代码的速度。我编写了一些代码来测试和理解并发期货并模拟我的案例。但是并发期货方法在某种程度上比循环要慢。

import concurrent.futures
import time

A = range(0,100000)
B = range(1000,20000)
SumList = []
DiffList = []
SumList2 = []
DiffList2 = []

def Funktion(items):
    A, B = items
    Sum = A + B
    Diff = A-B
    return(Sum, Diff)

def Funktion2(A, B):
    Sum = A+B
    Diff = A-B
    return(Sum, Diff)

start1 = time.time()
with concurrent.futures.ProcessPoolExecutor() as executor:
    for Sum, Diff in executor.map(Funktion, zip(A, B)):

        SumList.append(Sum)
        DiffList.append(Diff)


stop1 = time.time()
time1 = stop1 - start1
print('With Executor=', time1)

start2 = time.time()
i = 0
for x in A:
    Sum, Diff = Funktion2(A[i], B[i])
    SumList2.append(Sum)
    DiffList2.append(Diff)

stop2 = time.time()
time2 = stop2 - start2
print('Normal= ', time2)

结果是: 与执行器= 5.426360130310059 正常= 0.08866691589355469

以退出代码0结束的过程

0 个答案:

没有答案