I attempted to speed up my python program using the multiprocessing module but I found it was quite slow. A Toy example is as follows:
import time from multiprocessing import Pool, Manager class A: def __init__(self, i): self.i = i def score(self, x): return self.i - x class B: def __init__(self): self.i_list = list(range(1000)) self.A_list = [] def run_1(self): for i in self.i_list: self.x = i map(self.compute, self.A_list) #map version self.A_list.append(A(i)) def run_2(self): p = Pool() for i in self.i_list: self.x = i p.map(self.compute, self.A_list) #multicore version self.A_list.append(A(i)) def compute(self, some_A): return some_A.score(self.x) if __name__ == "__main__": st = time.time() foo = B() foo.run_1() print("Map: ", time.time()-st) st = time.time() foo = B() foo.run_2() print("MultiCore: ", time.time()-st) The outcomes on my computer(Windows 10, Python 3.5) is
Map: 0.0009996891021728516
MultiCore: 19.34994912147522
Similar results can be observed on Linux Machine (CentOS 7, Python 3.6).
I guess it was caused by the pickling/depicking of objects among processes? I tried to use the Manager module but failed to get it to work.
Any help will be appreciated.
mapthat is slow, it seems to be theself.A_list.append(A(i))Also you seem to usemapincorrectly. It returns a value and you are not using it at all. Do you know whatmapis doing?mapandappendare fast as can be seen from the first timing result. On the contrary,p.mapis slow(the multiprocessing version). I did not use the return value ofmapbecause this is just an example to show the poor performance ofp.mapand I did not need the return value.