I tried to compare the performance gain from parallel computing using multithreading module and the normal sequence computing but couldn't find any real difference. Here's what I did:
import time, threading, Queue
q=Queue.Queue()
def calc(_range):
exponent=(x**5 for x in _range)
q.put([x**0.5 for x in exponent])
def calc1(_range):
exponent=(x**5 for x in _range)
return [x**0.5 for x in exponent]
def multithreds(threadlist):
d=[]
for x in threadlist:
t=threading.Thread(target=calc, args=([x]))
t.start()
t.join()
s=q.get()
d.append(s)
return d
threads=[range(100000), range(200000)]
start=time.time()
#out=multithreads(threads)
out1=[calc1(x)for x in threads]
end=time.time()
print end-start
Timing using threading:0.9390001297
Timing running in sequence:0.911999940872
The timing running in sequence was constantly lower than using multithreading. I have a feeling there's something wrong with my multithreading code.
Can someone point me in the right direction please. Thanks.
calcfunctions?forking or starting threads isn't free, so for very small calls, the initializing overhead is very non-trivial compared to the actual "work."os.forkis too expensive for my main task. Thanks anyway.