Skip to content

Instantly share code, notes, and snippets.

@zafarali
Created June 2, 2016 21:09
Show Gist options
  • Save zafarali/d75b46c5038037af355543a8828c1ea5 to your computer and use it in GitHub Desktop.
Save zafarali/d75b46c5038037af355543a8828c1ea5 to your computer and use it in GitHub Desktop.
import numpy as np
import time
# the population:
global population
population = np.random.normal(-10,10, size=(10**6, 3))
def sample(args):
centre, radii = args
# print centre, radii
return np.sum((( population - np.array(centre))**2)/np.array((radii,)*3)**2, axis=1) <= 1
# centres:
centres = np.random.randint(-10,10, size=(20, 3))
# radii
radii = np.random.randint(2,5, size=20)
from multiprocessing import Pool
pool = Pool(2)
start = time.time()
results = pool.map(sample, zip(centres, radii))
end = time.time()
print 'parralelelized took:'
print 'took:'+str(end-start)
print(map(lambda res: np.sum(res), results))
print 'non parallelzied took:'
start = time.time()
for arg in zip(centres, radii):
sample(arg)
end = time.time()
print 'took:'+str(end-start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment