-
Notifications
You must be signed in to change notification settings - Fork 21
/
duration_comparison.py
47 lines (36 loc) · 1.14 KB
/
duration_comparison.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import time
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
import matplotlib.pyplot as plt
import numpy as np
def multithreading(func, args, workers):
with ThreadPoolExecutor(workers) as ex:
res = ex.map(func, args)
return list(res)
def multiprocessing(func, args, workers):
with ProcessPoolExecutor(workers) as ex:
res = ex.map(func, args)
return list(res)
def cpu_heavy(x):
print('I am', x)
start = time.time()
count = 0
for i in range(10**8):
count += i
stop = time.time()
return start, stop
def visualize_runtimes(results, title):
start, stop = np.array(results).T
plt.barh(range(len(start)), stop - start)
plt.grid(axis='x')
plt.ylabel("Tasks")
plt.xlabel("Seconds")
plt.xlim(0, 22.5)
ytks = range(len(results))
plt.yticks(ytks, ['job {}'.format(exp) for exp in ytks])
plt.title(title)
return stop[-1] - start[0]
plt.subplot(1, 2, 1)
visualize_runtimes(multithreading(cpu_heavy, range(4), 4), "Multithreading")
plt.subplot(1, 2, 2)
visualize_runtimes(multiprocessing(cpu_heavy, range(4), 4), "Multiprocessing")
plt.show()