views:

65

answers:

2

As part of a pet project of mine, I need to test the performance of various different implementations of my code in Python. I anticipate this to be something I do alot of, and I want to try to make the code I write to serve this aim as easy to update and modify as possible.

It's still in its infancy at the moment, but I've taken to using strings to manage common setup or testing code, eg:

naiveSetup = 'from PerformanceTests.Vectors import NaiveVector\n' \
+ 'left = NaiveVector([1,0,0])\n' \
+ 'right = NaiveVector([0,1,0])'

This allows me to only write the code once, at the expense of making it harder to read and clunky to update.

Is there a better way?

+2  A: 

Use triple quotes """

setup_code = """
  from PerformanceTests.Vectors import NaiveVector
  left = NaiveVector([1,0,0])
  right = NaiveVector([0,1,0])
"""

Another interesting method is provided in the docs of timeit:

def test():
    "Stupid test function"
    L = []
    for i in range(100):
        L.append(i)

if __name__=='__main__':
    from timeit import Timer
    t = Timer("test()", "from __main__ import test")
    print t.timeit()

Though this isn't suitable for all needs.

Eli Bendersky
A: 

Timing code is fine, but it will still leave you guessing what's going on.

To find out what's actually going on, manually pause it a few random times in the debugger, and examine the call stack.

For example, in the code that is 30x slower in one implementation than in another, each sample of the stack has a 96.7% chance of falling in the extra time that it is spending, so you can see why.

No guesswork required.

Mike Dunlavey
that's absolutely irrelevant to the question asked.
SilentGhost
This is an novel approach, but I'm not sure why you would perform 'profiling' this way when you have not 1 but 3 (at last count) profilers included with Python: cProfile, profile, and hotshot. See http://docs.python.org/library/profile.html#instant-user-s-manual
Peter Rowell
@SilentGhost: The OP said "I need to test the performance of various different implementations".
Mike Dunlavey
@Peter Rowell: Just because profilers are "out there" doesn't mean the subject is closed. Most profilers (including the ones you mentioned) are based the concepts in **gprof**, namely instrumentation and program-counter sampling. Here's an explanation of the problems with those: http://stackoverflow.com/questions/1777556/alternatives-to-gprof/1779343#1779343
Mike Dunlavey
... It is novel and not well-known, but if you try it you may come to see (as I and others have) that the difference between instrumentation and stackshots is like the difference between watching a sport and playing it. It's like the difference between monochrome and color. All that business about accuracy and measurement is like looking at the problem in 2D, when what you need is 3D. There's no comparison.
Mike Dunlavey
... It is novel but not new. My first memory of using it predates the original **gprof** paper, and I've heard of others just "inventing" it on their own. It just never got picked up and amplified by the academic echo-chamber, as **gprof** did. I only know of one actual product that does it (there may be more). That is Zoom by RotateRight.
Mike Dunlavey