Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def time_it(f, *args):
- start = time.clock()
- f(*args)
- return (time.clock() - start)*1000
- from timeit import Timer
- # first argument is the code to be run, the second "setup" argument is only run once,
- # and it not included in the execution time.
- t = Timer("""x.index(123)""", setup="""x = range(1000)""")
- print t.timeit() # prints float, for example 5.8254
- # ..or..
- print t.timeit(1000) # repeat 1000 times instead of the default 1million
- total= 0
- for i in range(1000):
- start= time.clock()
- function()
- end= time.clock()
- total += end-start
- time= total/1000
- start= time.clock()
- for i in range(1000):
- function()
- end= time.clock()
- time= (end-start)/1000
- import time
- class Timer:
- def __enter__(self):
- self.start = time.clock()
- return self
- def __exit__(self, *args):
- self.end = time.clock()
- self.interval = self.end - self.start
- import httplib
- with Timer() as t:
- conn = httplib.HTTPConnection('google.com')
- conn.request('GET', '/')
- print('Request took %.03f sec.' % t.interval)
- try:
- with Timer() as t:
- conn = httplib.HTTPConnection('google.com')
- conn.request('GET', '/')
- finally:
- print('Request took %.03f sec.' % t.interval)
- from contextlib import contextmanager
- import time
- @contextmanager
- def timeblock(label):
- start = time.clock()
- try:
- yield
- finally:
- end = time.clock()
- print ('{} : {}'.format(label, end - start))
- with timeblock("just a test"):
- print "yippee"
- import timeit
- timeit.timeit(fun, number=10000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement