-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultithreading.py
More file actions
66 lines (42 loc) · 2.42 KB
/
multithreading.py
File metadata and controls
66 lines (42 loc) · 2.42 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import concurrent.futures
import time
import numpy as np
import argparse
def find_hypotenuse_with_theading() -> tuple:
parser = argparse.ArgumentParser(description="Find hypotenuse")
parser.add_argument("cathetus_square_1", type=float, help="Input first cathetus: ")
parser.add_argument("cathetus_square_2", type=float, help="Input second cathetus: ")
args = parser.parse_args()
start_time = time.time()
with concurrent.futures.ProcessPoolExecutor(max_workers=12) as executor:
cathetus_square_1 = executor.submit(np.power, args.cathetus_square_1, 2)
cathetus_square_2 = executor.submit(np.power, args.cathetus_square_2, 2)
hypotenuse = executor.submit(np.power, cathetus_square_1.result() + cathetus_square_2.result(), 0.5)
print("--- %.5f seconds ---" % (time.time() - start_time))
return "%.3f" % hypotenuse.result(), "%.3f" % np.power(args.cathetus_square_1, 2), \
"%.3f" % np.power(args.cathetus_square_2, 2)
def find_hypotenuse_with_sleep() -> tuple:
parser = argparse.ArgumentParser(description="Find hypotenuse")
parser.add_argument("cathetus_square_1", type=float, help="Input first cathetus: ")
parser.add_argument("cathetus_square_2", type=float, help="Input second cathetus: ")
args = parser.parse_args()
start_time = time.time()
time.sleep(2)
cathetus_square_1 = np.power(args.cathetus_1, 2)
cathetus_square_2 = np.power(args.cathetus_2, 2)
hypotenuse = np.power(cathetus_square_1 + cathetus_square_2, 0.5)
print("--- %.5f seconds ---" % (time.time() - start_time))
return "%.3f" % hypotenuse, "%.3f" % np.power(args.cathetus_1, 2), "%.3f" % np.power(args.cathetus_2, 2)
def find_hypotenuse() -> tuple:
parser = argparse.ArgumentParser(description="Find hypotenuse")
parser.add_argument("cathetus_square_1", type=float, help="Input first cathetus: ")
parser.add_argument("cathetus_square_2", type=float, help="Input second cathetus: ")
args = parser.parse_args()
start_time = time.time()
cathetus_square_1 = np.power(args.cathetus_1, 2)
cathetus_square_2 = np.power(args.cathetus_2, 2)
hypotenuse = np.power(cathetus_square_1 + cathetus_square_2, 0.5)
print("--- %.5f seconds ---" % (time.time() - start_time))
return "%.3f" % hypotenuse, "%.3f" % np.power(args.cathetus_1, 2), "%.3f" % np.power(args.cathetus_2, 2)
if __name__ == "__main__":
print(find_hypotenuse_with_theading())