Advertisement
here2share

# threading4vs10_timeit.py

Dec 5th, 2019
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | None | 0 0
  1. # threading4vs10_timeit.py
  2.  
  3. import time
  4. import threading
  5.  
  6. def background(f):
  7.     '''
  8.     a threading decorator
  9.     use @background above the function you want to run in the background
  10.     '''
  11.     def bg_f(*a, **kw):
  12.         threading.Thread(target=f, args=a, kwargs=kw).start()
  13.     return bg_f
  14. @background
  15. def counter(name, n):
  16.     '''show the millisecond name and count'''
  17.     for k in range(1, n+1):
  18.         t = float(str(time.clock()-start)[:9])
  19.         p.append([t, "{} count {}".format(name, k)])
  20.  
  21. # start the counters
  22. # note that with the @background decorator
  23.  
  24. p = []
  25. start = time.clock()
  26. for z in range(4):
  27.     counter("%d of 4 Threads "%(z+1), 250)
  28. t = [(float(str(time.clock())[:9]),'spooling 4')]
  29. p.sort()
  30. for z in p: print '{:.7f} \t {}'.format(z[0],z[1])
  31. print
  32.  
  33. p = []
  34. start = time.clock()
  35. for z in range(10):
  36.     counter("%s of 10 Threads "%(str(z+1).zfill(2)), 100)
  37. t += [(float(str(time.clock())[:9]),'spooling 10')]
  38. for z in p: print '{:.7f} \t {}'.format(z[0],z[1])
  39. t.sort()
  40. a,b = t
  41. a,a2 = a
  42. b,b2 = b
  43. print
  44. print 'At {}s, {} is {}x faster than {} which took {}s'.format(a,a2,str(b/a)[:6],b2,b)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement