def myfun(a): return a*2 p=Pool(5) k0=time.time() p.map(myfun,[1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10]) k1=time.time() print(k1-k0) k0=time.time() for i in [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10]: myfun(i) k1=time.time() print(k1-k0) I am using the multiprocessing package in python. So as you can see i have executed two different snippets of code separately.The first one that uses Pool.map takes more time than the second one which is executed serially. Can anyone explain to me why so? I thought the p.map() would be much faster. Is it not executed parallely?
Pool.map()but I can tell you for sure from Java or C, that on small data, no matter how many threads you are using, it will always run slower than with one thread, because it takes time to 'start' those threads and etc. Try running the code with a seriousfor, like thousands of steps and you will see some good results. If I'm wrong, I'm sorry, I'm just telling you what I've experienced in Java or C.