Skip to content
{{ message }}

Instantly share code, notes, and snippets.

# Nike-Prallow/search-for-primes.py

Forked from adsworth/search-for-primes.py
Last active Mar 24, 2021
corrected multi process version of the prime number counter from https://www.youtube.com/watch?v=hGyJTcdfR1E
 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)
to join this conversation on GitHub. Already have an account? Sign in to comment