I am trying to understand how to implement multithreading in python, and I found this fantastic tutorial
https://chriskiehl.com/article/parallelism-in-one-line
However, I am not able to obtain satisfying results in my own experiment. My code is the following:
from multiprocessing.dummy import Pool as ThreadPool
import numpy as np
import time
times = []
v = np.random.random(1000000)
def square(x):
return x**2
for i in range(1,12):
print(i)
pool = ThreadPool(i)
t0 = time.time()
results = pool.map(square, v)
times.append(time.time() - t0)
It is a very simple example, where I have to calculate the pointwise square of an array, and I try to do this using threading.
I would expect the runtime (stored in the times array) to decrease, however, I get what follows:
number of threads: 1. Runtime: 6.5756611824035645 sec.
number of threads: 2. Runtime: 6.879298686981201 sec.
number of threads: 3. Runtime: 6.04328989982605 sec.
number of threads: 4. Runtime: 6.422291040420532 sec.
number of threads: 5. Runtime: 7.579150676727295 sec.
number of threads: 6. Runtime: 6.63776707649231 sec.
number of threads: 7. Runtime: 7.188394069671631 sec.
number of threads: 8. Runtime: 7.101344108581543 sec.
number of threads: 9. Runtime: 6.952891111373901 sec.
number of threads: 10. Runtime: 7.298482894897461 sec.
number of threads: 11. Runtime: 7.356388807296753 sec.
I am not able to figure out why the runtime does not decrease with more threads. Is there something wrong with my implementation?