Skip to content

Instantly share code, notes, and snippets.

@calumroy
Last active August 29, 2015 14:27
Show Gist options
  • Save calumroy/d3b9bb644719173b52b0 to your computer and use it in GitHub Desktop.
Save calumroy/d3b9bb644719173b52b0 to your computer and use it in GitHub Desktop.

Python Profiling Techniques

  • 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):
  ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment