Package dics :: Module ThreadTest2
[hide private]
[frames] | no frames]

Source Code for Module dics.ThreadTest2

 1  #!/usr/bin/env python 
 2   
 3  #Let us profile code which uses threads 
 4  import thread 
 5  import time 
 6  from threading import Thread 
 7   
8 -class MyThread(Thread):
9
10 - def __init__(self,bignum):
11 12 Thread.__init__(self) 13 self.bignum=bignum
14
15 - def run(self):
16 17 for l in range(10): 18 for k in range(self.bignum): 19 res=0 20 for i in range(self.bignum): 21 res+=1
22 23
24 -def myadd_nothread(bignum):
25 26 for l in range(10): 27 for k in range(bignum): 28 res=0 29 for i in range(bignum): 30 res+=1 31 32 for l in range(10): 33 for k in range(bignum): 34 res=0 35 for i in range(bignum): 36 res+=1
37
38 -def thread_test(bignum):
39 #We create 2 Thread objects for the 2 threads. 40 thr1=MyThread(bignum) 41 thr2=MyThread(bignum) 42 43 thr1.start() 44 thr2.start() 45 46 thr1.join() 47 thr2.join()
48 49
50 -def test():
51 52 bignum=1000 53 54 #Let us test the threading part 55 56 57 starttime=time.clock() 58 thread_test(bignum) 59 stoptime=time.clock() 60 61 print "Running 2 threads took %.3f seconds" % (stoptime-starttime) 62 63 #Now run without Threads. 64 starttime=time.clock() 65 myadd_nothread(bignum) 66 stoptime=time.clock() 67 68 print "Running Without Threads took %.3f seconds" % (stoptime-starttime)
69 70 71 if __name__=="__main__": 72 73 test() 74