Search code examples
pythontimemultiprocessingreducepool

Multiprocessing pool in python took more time


So, I started learning multiprocessing in python. I created a pool for function 'res'. I was interested in time after run program using the pool and using normal way, I thought that if I use pool processing time would be reduced but as I see, pool took 10.0413179397583 sec(s) and normal way took 0.005002737045288086 sec(s). What did I miss?

import multiprocessing as mp
import time

def res(a):
    squ = 0
    for i in range(a):
        squ += i**2
    return squ

if __name__ == "__main__":

    t1 = time.time()
    p = mp.Pool()
    result = p.map(res, range(10000))
    p.close()
    p.join()
    print(time.time()-t1)

    t2 = time.time()
    result = []
    sum = 0
    for i in range(10000):
        sum += i**2
        result.append(sum)
    print(time.time()-t2)

Solution

  • The algorithm you use with multiprocessing is O(n^2) (loop of 1, loop of 2, ... loop of 9999), while the "normal approach" is O(n). Without multiprocessing, the first way took about 3 times longer in my tests.

    Related: What is a plain English explanation of “Big O” notation?