Skip to content

Instantly share code, notes, and snippets.

@ebongzzang
Created October 21, 2019 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ebongzzang/ab2b5e75d978430b42c2ca005306f217 to your computer and use it in GitHub Desktop.
Save ebongzzang/ab2b5e75d978430b42c2ca005306f217 to your computer and use it in GitHub Desktop.
mp example
from itertools import chain
import numpy as np
from collections import Counter
from multiprocessing import cpu_count, Pool
def a(weights):
w_sum = np.sum(weights)
value = np.random.randint(w_sum, size=1)[0]
step = 0
index = 0
for w in weights:
if step <= value <= step + w:
return index
step += w
index += 1
input_value = np.array([10, 20, 30, 40])
def test_func(x):
return [a(input_value) for b in x]
if __name__ == '__main__':
import time
start_time = time.time()
cores = cpu_count() # Number of CPU cores on your system
partitions = cores # Define as many partitions as you want
print(cores)
with Pool(cores) as pool:
parts = np.array_split(range(100000000), cores)
cts = chain.from_iterable(pool.imap(test_func, parts))
pool.close()
pool.join()
res = Counter(cts)
print(res)
print(f"elapsed_time: {time.time() - start_time}")
total_sum = np.sum(list(res.values()))
ratio = {k: v / total_sum * 100 for k, v in res.items()}
print(ratio)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment