Last active
March 24, 2021 16:24
-
-
Save Nike-Prallow/072eb7079fdfbff3b824411e64db1123 to your computer and use it in GitHub Desktop.
corrected multi process version of the prime number counter from https://www.youtube.com/watch?v=hGyJTcdfR1E
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import multiprocessing as mp | |
import time | |
import pandas as pd | |
def chunks(seq, chunks): | |
size = len(seq) | |
start = 0 | |
for i in range(1, chunks + 1): | |
stop = i * size // chunks | |
yield seq[start:stop] | |
start = stop | |
def calc_primes(numbers): | |
num_primes = 0 | |
primes = [] | |
# Loop through each number, then through the factors to identify prime numbers | |
for candidate_number in numbers: | |
found_prime = True | |
for div_number in range(2, candidate_number): | |
if candidate_number % div_number == 0: | |
found_prime = False | |
break | |
if found_prime: | |
primes.append(candidate_number) | |
num_primes += 1 | |
return num_primes | |
def main(pnum=mp.cpu_count(), cnum=mp.cpu_count() * 16, maxNum=10000): | |
# Record the test start time | |
start = time.time() | |
pool = mp.Pool(pnum) | |
# 0 and 1 are not primes | |
parts = chunks(range(2, maxNum, 1), cnum) | |
# run the calculation | |
results = pool.map(calc_primes, parts) | |
total_primes = sum(results) | |
pool.close() | |
# Once all numbers have been searched, stop the timer | |
end = round(time.time() - start, 2) | |
# Display the results, uncomment the last to list the prime numbers found | |
profilingStats = { | |
'primeNumbers': maxNum, | |
'processCount': pnum, | |
'chunkNumber': cnum, | |
'time': end | |
} | |
return profilingStats | |
if __name__ == "__main__": | |
stats = list() | |
for m in [10, 100, 200, 500]: # set your maximalSizes to iterate over | |
for p in [1, 2, 4, 8]: # set your numbers of processes per physical CPU thread here | |
for c in [1, 2, 4]: # set your chunk size per process here | |
stats.append(main(p * mp.cpu_count(), p * c * mp.cpu_count(), m * 1000)) | |
print(stats[-1]) | |
data = pd.DataFrame.from_records(stats) | |
data.to_csv("data.csv", index=False, header=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment