I was trying to understand the use of map
with multiprocessing
. I have written the following python program for that purpose. But the results seems to confuse me.
from multiprocessing import Pool
import time
def f(x):
return x*x
if __name__ == '__main__':
p = Pool(5)
l = [x for x in range(2000000)]
start = time.clock()
p.map(f, l)
end = time.clock()
print('pool processing time {}'.format(end - start))
start = time.clock()
map(f, l)
end = time.clock()
print('sequential processing time {}'.format(end - start))
The output that I get is given below.
pool processing time 5.576627
sequential processing time 3.220387
Why is the sequential processing time greater than pool processing time? I am running this code on a Linux (Ubuntu 14.04 VM) that has two CPU's allocated to it.