[TIP] Use unittests for profiling?

Ryan Freckleton ryan.freckleton at gmail.com
Thu May 13 08:26:17 PDT 2010

On Thu, May 13, 2010 at 8:24 AM, Matthew Wilson <matt at tplus1.com> wrote:
> 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.
> Matt

(Sorry for the double post Matthew, I hit 'reply' instead of 'reply all')

You may want to see if you can glean the information you need from the
nosetests --with-profile output. I think it's designed to speed up
your unit tests, but you can just ignore the top level calls and look
at the execution time of your pieces of code.

There's definitely a library to do it with jUnit tests, so it's valid,
at least from the Java world.

--Ryan E. Freckleton

More information about the testing-in-python mailing list