Python Multiprocessing - execution time increased, what am I doing wrong?
<p>I'm doing a simple multiprocessing test and something seems off. Im running this on i5-6200U 2.3 Ghz with Turbo Boost.</p>

<pre><code>from multiprocessing import Process, Queue
import time

def multiply(a,b,que): #add a argument to function for assigning a queue
    que.put(a*b) #we're putting return value into queue

if __name__ == '__main__':
    queue1 = Queue() #create a queue object
    jobs = []
    start_time = time.time()
#####PARALLEL####################################
    for i in range(0,400):
        p = p = Process(target= multiply, args= (5,i,queue1))
        jobs.append(p)
        p.start()

    for j in jobs:
        j.join()

    print("PARALLEL %s seconds ---" % (time.time() - start_time))
#####SERIAL################################
    start_time = time.time()
    for i in range(0,400):
        multiply(5,i,queue1)
    print("SERIAL %s seconds ---" % (time.time() - start_time))
</code></pre>

<p>Output:</p>

<pre><code>PARALLEL 22.12951421737671 seconds ---
SERIAL 0.004009723663330078 seconds ---
</code></pre>

<p>Help is much appreciated.</p>

<p>Here's a brief example of (silly) code that gets a nice speedup.  As already covered in comments, it doesn't create an absurd number of processes, and the work done per remote function invocation is high compared to interprocess communication overheads.</p>

<pre><code>import multiprocessing as mp
import time

def factor(n):
    for i in range(n):
        pass
    return n

if __name__ == "__main__":
    ns = range(100000, 110000)

    s = time.time()
    p = mp.Pool(4)
    got = p.map(factor, ns)
    print(time.time() - s)
    assert got == list(ns)

    s = time.time()
    got = [factor(n) for n in ns]
    print(time.time() - s)
    assert got == list(ns)
</code></pre>

python multiprocessing python-multiprocessing