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?