Skip to content

Instantly share code, notes, and snippets.

@natec425
Created May 24, 2018 01:14
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 natec425/58a1c94ce5fca66e66a233d2a942c9e9 to your computer and use it in GitHub Desktop.
Save natec425/58a1c94ce5fca66e66a233d2a942c9e9 to your computer and use it in GitHub Desktop.
starting point for discussing sequential vs concurrent vs parallel
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