For class we were to make a MapReduce program in Python to find low value hashes of our name. I have completed the assignment but want to try and speed it up. The program currently takes about 45s to complete. I would like to see if there are any suggestions on speeding it up some.
The requirements are to find hashes of your name with 5 leading 0's when printed in hex. We are to try 40 million nonces. I did a few naive implementations before I finally settled on what is below. What I do is send a dict of 40 consecutive numbers to use as multipliers in the Map function. The multiplier represents the range of millions to go through. So when mult = 0 I will use the nonces 0-1mil, when mult = 23 use the nonces 23mil-24mil.
#!/usr/bin/env python import mincemeat def mapfn(k, v): #Hash the string with the given nonce, if its good save it import md5 #Create a md5 hash and fill it with out initial value #The "blockheader" in Bitcoin terms m = md5.new() m.update("Kevin") #Now, step through 1 million nonces with v as a multiplier for i in range(v*1000000, ((v+1)*1000000), 1): mPrime = m.copy() mPrime.update(str(i)) hashOut = mPrime.hexdigest() if(hashOut[:5] == '0' * 5): yield hashOut, i else: pass #Hash trash! def reducefn(k, vs): return (k, vs) if __name__ == "__main__": #Import some useful code import sys import collections #Build the data source, just a list 0-39 nonces = [i for i in range(0, 40)] datasource = dict(enumerate(nonces)) #Setup the MapReduce server s = mincemeat.Server() s.mapfn = mapfn s.reducefn = reducefn s.datasource = datasource #Get the results of the MapReduce results = s.run_server(password="changeme") #List the good hashes print "\nHashed on the string: Kevin\nResults..." for i in range(0, len(results)): key, value = results.popitem() hashStr, nonce = value print "Nonce: " + str(nonce) + ", hash: " + str(hashStr)