0

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?

Dominique
  • 16,450
  • 15
  • 56
  • 112
Ulderique Demoitre
  • 1,029
  • 3
  • 11
  • 26
  • multithreading won't help you speed up CPU intensive tasks in python. see e.g. [https://wiki.python.org/moin/GlobalInterpreterLock](https://wiki.python.org/moin/GlobalInterpreterLock) or https://stackoverflow.com/questions/20939299/does-python-support-multithreading-can-it-speed-up-execution-time. – hiro protagonist Feb 13 '19 at 10:45
  • Multi-threading is not always guaranteed to result in better performance. The context switch is a heavy operation and adds time. Also, only one thread at a time can run in Python's interpreter lock. There are libraries that provide multithreading support though. – Sid Feb 13 '19 at 10:45
  • So, the problem here seems to be the fact that several threads are trying to use at the same time the `square` function, with no benefit. Should I expect some benefit if I use multiprocessing instead? – Ulderique Demoitre Feb 13 '19 at 10:59
  • Read [multiprocessing-vs-threading-python](https://stackoverflow.com/questions/3044580/multiprocessing-vs-threading-python) – stovfl Feb 13 '19 at 13:54

0 Answers0