-
Notifications
You must be signed in to change notification settings - Fork 62
/
example1.py
56 lines (36 loc) · 1.2 KB
/
example1.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
48
49
50
51
52
53
54
55
56
# ch2/example1.py
from math import sqrt
import concurrent.futures
import multiprocessing
from timeit import default_timer as timer
def is_prime(x):
if x < 2:
return False
if x == 2:
return x
if x % 2 == 0:
return False
limit = int(sqrt(x)) + 1
for i in range(3, limit, 2):
if x % i == 0:
return False
return x
def concurrent_solve(n_workers):
print('Number of workers: %i.' % n_workers)
start = timer()
result = []
with concurrent.futures.ProcessPoolExecutor(max_workers=n_workers) as executor:
futures = [executor.submit(is_prime, i) for i in input]
completed_futures = concurrent.futures.as_completed(futures)
sub_start = timer()
for i, future in enumerate(completed_futures):
if future.result():
result.append(future.result())
sub_duration = timer() - sub_start
duration = timer() - start
print('Sub took: %.4f seconds.' % sub_duration)
print('Took: %.4f seconds.' % duration)
input = [i for i in range(10 ** 13, 10 ** 13 + 1000)]
for n_workers in range(1, multiprocessing.cpu_count() + 1):
concurrent_solve(n_workers)
print('_' * 20)