I've found the threading module takes more time than multiprocessing for the same task.
import time
import threading
import multiprocessing
def func():
result = 0
for i in xrange(10**8):
result += i
num_jobs = 10
# 1. measure, how long it takes 10 consistent func() executions
t = time.time()
for _ in xrange(num_jobs):
func()
print ("10 consistent func executions takes:\n{:.2f} seconds\n".format(time.time() - t))
# 2. threading module, 10 jobs
jobs = []
for _ in xrange(num_jobs):
jobs.append(threading.Thread(target = func, args=()))
t = time.time()
for job in jobs:
job.start()
for job in jobs:
job.join()
print ("10 func executions in parallel (threading module) takes:\n{:.2f} seconds\n".format(time.time() - t))
# 3. multiprocessing module, 10 jobs
jobs = []
for _ in xrange(num_jobs):
jobs.append(multiprocessing.Process(target = func, args=()))
t = time.time()
for job in jobs:
job.start()
for job in jobs:
job.join()
print ("10 func executions in parallel (multiprocessing module) takes:\n{:.2f} seconds\n".format(time.time() - t))
Results:
10 consistent func executions takes:
25.66 seconds
10 func executions in parallel (threading module) takes:
46.00 seconds
10 func executions in parallel (multiprocessing module) takes:
7.92 seconds
1) Why the implementation with multiprocessing module works better than with threading module?
2) Why consistent func executions takes less time then using threading module?