-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_thread_test.py
More file actions
61 lines (51 loc) · 1.68 KB
/
process_thread_test.py
File metadata and controls
61 lines (51 loc) · 1.68 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding: utf-8 -*-
import time
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
import matplotlib.pyplot as plt
import numpy as np
import random
import string
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 io_heavy(text):
start = time.time()
f = open('output.txt', 'wt', encoding='utf-8')
f.write(text)
f.close()
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, 0.1)
ytks = range(len(results))
plt.yticks(ytks, ['job {}'.format(exp) for exp in ytks])
plt.title(title)
return stop[-1] - start[0]
if __name__ == '__main__':
N=12
TEXT = ''.join(random.choice(string.ascii_lowercase) for i in range(10**7*5))
plt.subplot(1, 2, 1)
# visualize_runtimes(multithreading(cpu_heavy, range(4), 4), "Multithreading")
visualize_runtimes(multithreading(io_heavy, [TEXT for i in range(N)], 1),"Single Thread")
plt.subplot(1, 2, 2)
# visualize_runtimes(multiprocessing(cpu_heavy, range(4), 4), "Multiprocessing")
visualize_runtimes(multiprocessing(io_heavy, [TEXT for i in range(N)], 1),"Single Process")
plt.show()