I'm just getting aquanted with multiprocessing (as opposed to threading, this is supposed to leverage the multiple cores available). I'm using some dummy calculations to test speed:
def g(number):
f = (math.factorial(number))
f = f * 10
Then I try to call the function sequentially while measuring time:
start_time = time.time()
g(10000)
g(2000)
g(3000)
print("--- %s seconds ---" % (time.time() - start_time))
This returns --- 0.003850698471069336 seconds ---
Now when I'm trying multiprocessing either via
start_time_ = time.time()
p = Pool(3)
p.map(g, [10000,2000,3000])
print("--- %s seconds ---" % (time.time() - start_time_))
or
start_time_1 = time.time()
pool = Pool(processes=3)
pool.map(g, [10000,2000,3000])
print("--- %s seconds ---" % (time.time() - start_time_1))
I get a much worse running time, --- 0.0334630012512207 seconds ---
and --- 0.6992270946502686 seconds ---
.
Why is running the 3 calculations on 3 different cores/processes result in a bigger calculation time? Am I doing something wrong? Thank you!