Skip to content

Instantly share code, notes, and snippets.

@atmb4u
Last active December 27, 2016 18:09
Show Gist options
  • Save atmb4u/3ad6f21d62cd398f8f006043fa5ae708 to your computer and use it in GitHub Desktop.
Save atmb4u/3ad6f21d62cd398f8f006043fa5ae708 to your computer and use it in GitHub Desktop.
A quick and dirty CPU benchmarking in python
from hashlib import md5
from multiprocessing import Pool
from time import time
"""
A quick and dirty CPU benchmarking in python
Calculates md5 for a text multiple times in
multiple processes to push CPU to its limits.
"""
def digest(n):
"""
create a md5 hexdigest for abracadabra
"""
string = "abracadabra"
for x in xrange(1,n):
string = md5(string).hexdigest()
def benchmark():
"""
Create a multiprocessing.Pool for creating the digest
in multiple processes to fully blow out all the CPUs available.
32 processes, 100 times digest for 100,000 loops.
"""
start = time()
number_of_processes = 32
pool = Pool(number_of_processes)
pool.map(digest, ([100000] * 100))
end = time()
print "Time took in seconds (lower the better): %s (%.2f x)" % (str(end - start), (6.2/(end-start)))
print "It took 6.2 sec on a MacBook Pro (Early 2015, 2.9GHz i5, 16GB)"
if __name__ == "__main__":
benchmark()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment