import time
from multiprocessing import Pool
import multiprocessing as multi
def process(i):
return [{'id': j, 'sum': sum(range(i*j))} for j in range(1000)]
print("normal")
s = time.time()
for i in range(20):
process(i)
elapsed = time.time() - s
print(elapsed)
print("multi")
s = time.time()
p = Pool(multi.cpu_count())
p.map(process, list(range(20)))
p.close()
elapsed = time.time() - s
print(elapsed)
通常のループ
for i in [0, 1, 2]:
process(i)
並列ループ
p = Pool(multi.cpu_count())
p.map(process, [0, 1, 2])
p.close()