-
Notifications
You must be signed in to change notification settings - Fork 417
/
cf.py
35 lines (30 loc) · 914 Bytes
/
cf.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
from concurrent import futures
import math
import time
import sys
def calc(val):
time.sleep(1)
result = math.sqrt(float(val))
return result
def use_threads(num, values):
t1 = time.time()
with futures.ThreadPoolExecutor(num) as tex:
results = tex.map(calc, values)
t2 = time.time()
return t2 - t1
def use_processes(num, values):
t1 = time.time()
with futures.ProcessPoolExecutor(num) as pex:
results = pex.map(calc, values)
t2 = time.time()
return t2 - t1
def main(workers, values):
print(f"Using {workers} workers for {len(values)} values")
t_sec = use_threads(workers, values)
print(f"Threads took {t_sec:.4f} seconds")
p_sec = use_processes(workers, values)
print(f"Processes took {p_sec:.4f} seconds")
if __name__ == '__main__':
workers = int(sys.argv[1])
values = list(range(1, 6)) # 1 .. 5
main(workers, values)