- timeit Is useful for quickly testing self contained functions.
import timeit
timeit.timeit('text.find(char)', setup='text = "sample string"; char = "g"')
Can also test class functions but you have to import the class and variables using setup.
import timeit
def test():
"""Stupid test function"""
L = []
for i in range(100):
L.append(i)
if __name__ == '__main__':
import timeit
print(timeit.timeit("test()", setup="from __main__ import test"))
- cprofile usefule for timing funtions inside classes. You can import all the local and global variables to the Cprofile. eg.
import cProfile as profile
class Foo(object):
... def bar(self):
... profile.runctx('self.baz()', globals(), locals())
...
... def baz(self):
... time.sleep(1)
... print 'slept'
... time.sleep(2)
...
>>> foo = Foo()
>>> foo.bar()
Cprofile can also be used to define a timing function.
# Profiling function
def do_cprofile(func):
def profiled_func(*args, **kwargs):
profile = cProfile.Profile()
try:
profile.enable()
result = func(*args, **kwargs)
profile.disable()
return result
finally:
profile.print_stats()
return profiled_func
Then just put the timing function above any functions you want to time.
class HTM:
@do_cprofile # For profiling
def __init__(self, input, params):
...