How come the multi function that uses a multiprocessing pool to segment and process data on multiple "processes" is slower (8 seconds) than just calling the map function (6 seconds)?
from multiprocessing import Pool
import timeit
def timer(function):
def new_function():
start_time = timeit.default_timer()
function()
elapsed = timeit.default_timer() - start_time
print('Function "{name}" took {time} seconds to complete.'.format(name=function.__name__, time=elapsed))
return new_function
def cube(n):
return n*n*n
nums = range(20000000)
if __name__ == '__main__':
@timer
def multi():
pool = Pool()
res = pool.map(cube,nums)
pool.close()
pool.join()
@timer
def test():
a = map(cube,nums)
multi()
test()