-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
48 lines (32 loc) · 975 Bytes
/
test.py
File metadata and controls
48 lines (32 loc) · 975 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
43
44
45
46
47
def fib(n):
if n <= 1:
return n
return fib(n - 1) + fib(n - 2)
import threading
import time
def run_threads():
start_time = time.time()
threads = []
for _ in range(2): # Two threads, but still runs sequentially due to GIL
t = threading.Thread(target=fib, args=(35,))
threads.append(t)
t.start()
for t in threads:
t.join()
print(f"Threads Execution Time: {time.time() - start_time:.2f} seconds")
if __name__ == "__main__":
run_threads()
import multiprocessing
import time
def run_processes():
start_time = time.time()
processes = []
for _ in range(2): # Two processes, true parallel execution
p = multiprocessing.Process(target=fib, args=(35,))
processes.append(p)
p.start()
for p in processes:
p.join()
print(f"Processes Execution Time: {time.time() - start_time:.2f} seconds")
if __name__ == "__main__":
run_processes()