forked from PacktPublishing/AdvancedPythonProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample1.py
More file actions
51 lines (37 loc) · 1.04 KB
/
example1.py
File metadata and controls
51 lines (37 loc) · 1.04 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
# ch1/example1.py
from math import sqrt
import concurrent.futures
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
input = [i for i in range(10 ** 13, 10 ** 13 + 500)]
# sequential
# comment out to only run concurrent
start = timer()
result = []
for i in input:
if is_prime(i):
result.append(i)
print('Result 1:', result)
print('Took: %.2f seconds.' % (timer() - start))
# concurrent
# comment out to only run sequential
start = timer()
result = []
with concurrent.futures.ProcessPoolExecutor(max_workers=20) as executor:
futures = [executor.submit(is_prime, i) for i in input]
for i, future in enumerate(concurrent.futures.as_completed(futures)):
if future.result():
result.append(future.result())
print('Result 2:', result)
print('Took: %.2f seconds.' % (timer() - start))