Posts Tagged ‘decorator’

Timing Decorator

Thursday, February 12th, 2009

I noticed I left some of the timing decorator @wrapper statements in some solutions, so here is the timing function I have been using to determine the approximate solution time.

?View Code PYTHON
def print_timing(func):
  def wrapper(*arg):
    t1 = time.time()
    res = func(*arg)
    t2 = time.time()
    print '%s took %0.3f ms' % (func.func_name, (t2-t1)*1000.0)
    return res
  return wrapper

In use:

?View Code PYTHON
@print_timing
def some_function()
  return "Hi, I'm a function. And I'm gonna get timed!"