-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompute_example.py
More file actions
41 lines (32 loc) · 976 Bytes
/
Copy pathcompute_example.py
File metadata and controls
41 lines (32 loc) · 976 Bytes
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
import datetime
import multiprocessing
from threading import Thread
import math
def do_math(start=0, num=10):
print(f'start: {start} ----------- num: {num} ////')
position = start
random_number = 1000 * 1000
while position < num:
position += 1
math.sqrt((position - random_number) ** 2)
def main():
t0 = datetime.datetime.now()
threads = []
processor_count = multiprocessing.cpu_count()
for n in range(1, processor_count + 1):
threads.append(
Thread(
target=do_math,
kwargs={
'start': 50_000_000 * (n - 1) / processor_count,
'num': 50_000_000 * n / processor_count
},
daemon=True
)
)
[t.start() for t in threads]
[t.join() for t in threads]
dt = datetime.datetime.now() - t0
print(f'Done in {dt.total_seconds()} seconds')
if __name__ == '__main__':
main()