[TIP] Use unittests for profiling?

Matthew Wilson matt at tplus1.com
Thu May 13 07:24:23 PDT 2010

I've been writing lots of snippets of code and then feeding them into
cProfile and timeit, looking for stuff to optimize.  It dawned on me
recently that these snippets look a lot like some of my unittest
classes: each snippet has a few statements and an optional setup
block.  In both cases, I'm isolating a component from a bigger system
and then running just that component.

Has anyone done any work to reuse unit tests for profiling and timing?

Is there something theoretically wrong with this idea?

There's a practical hurdle -- the cProfile and timeit modules were
originally designed to eat strings of code.  They can work with
callable objects, but I am finding that to be a little difficult when
I have non-trivial setups.


W. Matthew Wilson
matt at tplus1.com

More information about the testing-in-python mailing list