-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path5 proc_compare.py
58 lines (44 loc) · 1.53 KB
/
5 proc_compare.py
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
import time
import threading
import multiprocessing
import math
# A heavy CPU-bound function - calculating factorial of a large number
def calculate_factorial(number):
math.factorial(number)
# The large number for which factorial will be calculated
large_number = 20000
# The number of times the factorial will be calculated
n_calculations = 10
def main():
# Sequential processing
start_time = time.time()
for _ in range(n_calculations):
calculate_factorial(large_number)
end_time = time.time()
print("Sequential processing took: {:.6f} seconds".format(end_time - start_time))
# ----------------------------------------
# Multi-threading
start_time = time.time()
threads = []
for _ in range(n_calculations):
thread = threading.Thread(target=calculate_factorial, args=(large_number,))
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
end_time = time.time()
print("Multi-threading took: {:.6f} seconds".format(end_time - start_time))
# ----------------------------------------
# Multi-processing
start_time = time.time()
processes = []
for _ in range(n_calculations):
process = multiprocessing.Process(target=calculate_factorial, args=(large_number,))
process.start()
processes.append(process)
for process in processes:
process.join()
end_time = time.time()
print("Multi-processing took: {:.6f} seconds".format(end_time - start_time))
if __name__ == '__main__':
main()