Skip to content

Instantly share code, notes, and snippets.

@willtownes
Created July 12, 2016 17:01
Show Gist options
  • Save willtownes/2762ba1dd73fb03ecdb24d625aa27b58 to your computer and use it in GitHub Desktop.
Save willtownes/2762ba1dd73fb03ecdb24d625aa27b58 to your computer and use it in GitHub Desktop.
python multiprocessing benchmarks
"""
Testing python multiprocessing speed. Based on
http://blogs.warwick.ac.uk/dwatkins/entry/benchmarking_parallel_python_1_2/
"""
import time
import math
import multiprocessing as mp
def isprime(n):
"""Returns True if n is prime and False otherwise"""
if not isinstance(n, int):
raise TypeError("argument passed to is_prime is not of 'int' type")
if n < 2:
return False
if n == 2:
return True
max = int(math.ceil(math.sqrt(n)))
i = 2
while i <= max:
if n % i == 0:
return False
i += 1
return True
def sum_primes(n):
"""Calculates sum of all primes below given integer n"""
return sum([x for x in xrange(2,n) if isprime(x)])
if __name__=="__main__":
inputs = range(100000, 1000000, 100000)
n_cpu = mp.cpu_count()
pool = mp.Pool(n_cpu)
print("\nParallel processing with %d cores"%n_cpu)
start_time = time.time()
jobs = zip(inputs, pool.map(sum_primes,inputs))
for input, job in jobs:
print "Sum of primes below", input, "is", job
print "Time elapsed: ", time.time() - start_time, "s"
print("\nSerial processing")
start_time = time.time()
for x in inputs:
print "%d: %d" % (x, sum_primes(x))
print "Time elapsed: ", time.time() - start_time, "s"
@singhsidhukuldeep
Copy link

Gitpod
CPU: AMD EPYC 7B13
Arch: x86_64
OS: Linux

Parallel processing with 16 cores
Sum of primes below 100000 is 454396537
Sum of primes below 200000 is 1709600813
Sum of primes below 300000 is 3709507114
Sum of primes below 400000 is 6458901531
Sum of primes below 500000 is 9914236195
Sum of primes below 600000 is 14071826345
Sum of primes below 700000 is 18910286312
Sum of primes below 800000 is 24465663438
Sum of primes below 900000 is 30689332265
Time elapsed: 6.813516616821289 s

Serial processing
100000: 454396537
200000: 1709600813
300000: 3709507114
400000: 6458901531
500000: 9914236195
600000: 14071826345
700000: 18910286312
800000: 24465663438
900000: 30689332265

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment