-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathcompute_threaded.py
More file actions
42 lines (31 loc) · 936 Bytes
/
compute_threaded.py
File metadata and controls
42 lines (31 loc) · 936 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
42
import datetime
import math
import multiprocessing
from threading import Thread
def main():
do_math(1)
t0 = datetime.datetime.now()
# do_math(num=30000000)
print(f'Doing math on {multiprocessing.cpu_count():,} processors.')
processor_count = multiprocessing.cpu_count()
threads = []
for n in range(1, processor_count + 1):
threads.append(
Thread(
target=do_math,
args=(30_000_000 * (n - 1) / processor_count, 30_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():,.2f} sec.')
def do_math(start=0, num=10):
pos = start
k_sq = 1000 * 1000
while pos < num:
pos += 1
math.sqrt((pos - k_sq) * (pos - k_sq))
if __name__ == '__main__':
main()