File tree 1 file changed +17
-2
lines changed
src/05-threads/cpu_attempt
1 file changed +17
-2
lines changed Original file line number Diff line number Diff line change 1
1
import datetime
2
2
import math
3
+ from threading import Thread
4
+ import multiprocessing
3
5
4
6
5
7
def main ():
6
8
do_math (1 )
7
9
8
10
t0 = datetime .datetime .now ()
9
11
10
- do_math (num = 30000000 )
12
+ # do_math(num=30000000)
13
+ print ("Doing math on {:,} processors." .format (multiprocessing .cpu_count ()))
14
+
15
+ processor_count = multiprocessing .cpu_count ()
16
+ threads = []
17
+ for n in range (1 , processor_count + 1 ):
18
+ threads .append (Thread (target = do_math ,
19
+ args = (30_000_000 * (n - 1 ) / processor_count ,
20
+ 30_000_000 * n / processor_count ),
21
+ daemon = True )
22
+ )
23
+
24
+ [t .start () for t in threads ]
25
+ [t .join () for t in threads ]
11
26
12
27
dt = datetime .datetime .now () - t0
13
28
print ("Done in {:,.2f} sec." .format (dt .total_seconds ()))
@@ -18,7 +33,7 @@ def do_math(start=0, num=10):
18
33
k_sq = 1000 * 1000
19
34
while pos < num :
20
35
pos += 1
21
- dist = math .sqrt ((pos - k_sq )* (pos - k_sq ))
36
+ math .sqrt ((pos - k_sq ) * (pos - k_sq ))
22
37
23
38
24
39
if __name__ == '__main__' :
You can’t perform that action at this time.
0 commit comments