Skip to content

Instantly share code, notes, and snippets.

@kajuberdut
Last active July 13, 2017 21:20
Show Gist options
  • Save kajuberdut/f3783e097bacc8713206c4d7c033f650 to your computer and use it in GitHub Desktop.
Save kajuberdut/f3783e097bacc8713206c4d7c033f650 to your computer and use it in GitHub Desktop.
Multi Processor
from multiprocessing import Pool
from time import time
data = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve"]
progression = [1,2,4,6,12]
def do(name):
start = time()
i = 0
while time() - start <= 1:
i += 1
return i
if __name__ == "__main__":
for threads in progression:
print(f'threads: {threads}')
for slices in progression:
sliced = data[0:slices]
print(f"sliced: {sliced}")
start = time()
with Pool(threads) as p:
total = sum(p.map(do, sliced, 1))
print(f'{total} counted in {time() - start} seconds')
@kajuberdut
Copy link
Author

With 6 threads on 6 logical cores. I get about 30,500,000 iterations per second.
With 12 threads on 6 logical cores I get 37,500,000
So with this specific example it's about 22% more efficient leveraging hyper-threading to split cores.

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