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

Source Code for Module dics.ThreadTest3

 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 import sys 57 sys.setcheckinterval(1000) 58 59 60 starttime=time.time() 61 thread_test(bignum) 62 stoptime=time.time() 63 64 print "Running 2 threads took %.3f seconds" % (stoptime-starttime) 65 66 #Now run without Threads. 67 starttime=time.time() 68 myadd_nothread(bignum) 69 stoptime=time.time() 70 71 print "Running Without Threads took %.3f seconds" % (stoptime-starttime)
72 73 74 if __name__=="__main__": 75 76 test() 77