Last active
July 13, 2017 21:20
-
-
Save kajuberdut/f3783e097bacc8713206c4d7c033f650 to your computer and use it in GitHub Desktop.
Multi Processor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.