30/04/2007

A very simple profiler

I needed a very simple and quick profiler - this is what I came up with. Youll notice its very like a C define, oh well :)

from time import time

PROFILE_STACK = []

def BEGIN_PROFILE(thing):
    ind = "  " * len(PROFILE_STACK)
    PROFILE_STACK.append((thing, time()))
    print "%sBEGIN_PROF %s" % (ind, thing)

def END_PROFILE():
    thing, start = PROFILE_STACK.pop()
    ind = "  " * len(PROFILE_STACK)
    print "%sEND_PROF %s = %.3f" % (ind, thing, time()-start)

You simply wrap blocks of code in BEGIN_PROFILE('some string ident') and END_PROFILE() and it will print the execution time.

Im still fighting with pywracer physics unfortunatly and now Im vaguely tempted to try my hand at a golf game. There arent any free golf games! ( unless we count neverputt )

No comments: