Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # threading4vs10_timeit.py
- import time
- import threading
- def background(f):
- '''
- a threading decorator
- use @background above the function you want to run in the background
- '''
- def bg_f(*a, **kw):
- threading.Thread(target=f, args=a, kwargs=kw).start()
- return bg_f
- @background
- def counter(name, n):
- '''show the millisecond name and count'''
- for k in range(1, n+1):
- t = float(str(time.clock()-start)[:9])
- p.append([t, "{} count {}".format(name, k)])
- # start the counters
- # note that with the @background decorator
- p = []
- start = time.clock()
- for z in range(4):
- counter("%d of 4 Threads "%(z+1), 250)
- t = [(float(str(time.clock())[:9]),'spooling 4')]
- p.sort()
- for z in p: print '{:.7f} \t {}'.format(z[0],z[1])
- print
- p = []
- start = time.clock()
- for z in range(10):
- counter("%s of 10 Threads "%(str(z+1).zfill(2)), 100)
- t += [(float(str(time.clock())[:9]),'spooling 10')]
- for z in p: print '{:.7f} \t {}'.format(z[0],z[1])
- t.sort()
- a,b = t
- a,a2 = a
- b,b2 = b
- print
- 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