Created
May 24, 2018 01:14
-
-
Save natec425/58a1c94ce5fca66e66a233d2a942c9e9 to your computer and use it in GitHub Desktop.
starting point for discussing sequential vs concurrent vs parallel
This file contains hidden or 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 timeit import timeit | |
def smap(func, stuff): | |
return list(map(func, stuff)) | |
def cmap(func, stuff): | |
with pool.ThreadPool(processes=4) as p: | |
return p.map(func, stuff) | |
def pmap(func, stuff): | |
with pool.Pool(processes=4) as p: | |
return p.map(func, stuff) | |
def double(x): | |
return x * 2 | |
def time_map(which_map): | |
print( | |
timeit( | |
'map(double, nums)', | |
number=100, | |
globals={ | |
'map': which_map, | |
'double': double, | |
'nums': list(range(1000)) | |
})) | |
def main(): | |
for m in [smap, cmap, pmap]: | |
time_map(m) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment