1

I wanted to save the computation time using the `Threading` module in Python.
The task is to find 10 numbers which is generated in random uniform distribution in the range of (0, 1e7) and smaller than 1.
So I thought if I use 2 thread, the computation time will be reduced in half.
However, it doesn't seems to save time even I used 8 threads.

Why this happened?

Source

import random
import threading
import time

n_find = 0
start_time = time.time()

while True:
    if random.uniform(0, 1e7) < 1:
        n_find += 1

    if n_find >= 10:
        break
    
print(f'Complete: {time.time()-start_time}sec')

output
Complete: 32.8587543964386sec

Source (8 Threads)

start = False

n_find = 0
def find(thread_id):
    global n_find
    
    while not start:
        pass
    
    while True:
        if random.uniform(0, 1e7) < 1:
            n_find += 1
            
        if n_find >= 10:
            break
    
n_thread = 8
threads = [threading.Thread(target=find, args=(i+1, )) for i in range(n_thread)]

pre_time = time.time()
for i in range(n_thread):
    threads[i].start()
print(f'It takes {time.time()-pre_time}sec to start {n_thread} threads.')

start_time = time.time()
start = True

while True:
    if any([thread.isAlive() for thread in threads]):
        pass 
    else:
        break
        
print(f'Complete: {time.time()-start_time}sec')

output (8 Threads)
It took 0.46877384185791016sec to start 8 threads.
Complete: 25.215397119522095sec

p.s. how can I know the maximum and proper number of thread?

Kang San Lee
  • 312
  • 2
  • 10
  • 2
    What you want is multiprocessing, not threading. This is a CPU-bound task so spawning more threads does not give you any gains as the CPU is never context switching. – Maximilian Burszley Oct 20 '20 at 13:22
  • Have a look on this topic: https://stackoverflow.com/a/3046201/494631 – baldr Oct 20 '20 at 13:23
  • 1
    Welcome to the [GIL](https://wiki.python.org/moin/GlobalInterpreterLock)! – Klaus D. Oct 20 '20 at 13:23
  • 1
    @KlausD. Has nothing to do with the GIL. They're purely using the wrong tool for the job. – Maximilian Burszley Oct 20 '20 at 13:32
  • 1
    @MaximilianBurszley The GIL prevents that threads with CPU intensive Python code run at the same time. That means they can not make use of available free CPU cores and the application is effectively single CPU. Without the GIL every thread would be able to occupy one CPU core (or thread). This is why the typical the to run CPU intensive code in CPython is multi-processing, while in in other languages (and even interpreters) multi-threading in sufficient. – Klaus D. Oct 20 '20 at 18:02

0 Answers0