Parallel execution
concurrent.py
# -*- coding: utf-8 -*-
from benchit import BenchIt
from concurrent import futures
b = BenchIt()
def process_job(job):
count = 10000000
while count>0:
count -= 1
return job
jobs = [i for i in range(20)]
#No thread
result = []
for job in jobs:
result.append(process_job(job))
b.mark("Non-Thread")
#With thread
result = []
thread_pool = futures.ThreadPoolExecutor(max_workers=5)
result = thread_pool.map(process_job, jobs)
b.mark("Multi-thread5")
#With thread
result = []
thread_pool = futures.ThreadPoolExecutor(max_workers=10)
result = thread_pool.map(process_job, jobs)
b.mark("Multi-thread10")
b.display()
display.text
+----------------+----------+------+-------+----------+----------+---------+
| Marker | Method | Line | Loops | Avg Time | Runtime | Percent |
+----------------+----------+------+-------+----------+----------+---------+
| Non-Thread | <module> | 21 | 1 | 14.82156 | 14.82156 | 88.72 |
| Multi-thread5 | <module> | 28 | 1 | 0.18812 | 0.18812 | 1.13 |
| Multi-thread10 | <module> | 35 | 1 | 1.69720 | 1.69720 | 10.16 |
+----------------+----------+------+-------+----------+----------+---------+
Total runtime: 16.71
document Aim python library master (27) benchit
Recommended Posts