-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathos1.py
More file actions
32 lines (26 loc) · 708 Bytes
/
Copy pathos1.py
File metadata and controls
32 lines (26 loc) · 708 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
# cpu_threads.py
import threading
import time
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
def count_primes(start, end):
count = 0
for i in range(start, end):
if is_prime(i):
count += 1
print(f"Primes between {start}-{end}: {count}")
start = time.time()
threads = []
ranges = [(10_000, 50_000), (50_001, 90_000), (90_001, 130_000), (130_001, 170_000)]
for r in ranges:
t = threading.Thread(target=count_primes, args=r)
t.start()
threads.append(t)
for t in threads:
t.join()
print(f"Total time with threads: {time.time() - start:.2f} seconds")