[wingide-users] newbie question?

Tim Mitchell t.mitchell at aranz.com
Thu Sep 6 19:55:25 MDT 2007


Hi John,

I have written myself a decorator function for easy profiling. I have 
attached the module.
I'ts simple as:

import profiler

@profiler.decorate
def myfunc(...):
    ...

Cheers
Tim

johnf wrote:
> Is there a simple way to use wing 3.0 (linux) to profile a program?
>   

-- 
Tim Mitchell
Software Engineer
Applied Research Associates (NZ) Ltd. (www.aranz.com)
Ph: +64 (3) 374-6120 ext: 203
Fax: +64 (3) 374-6130
Skype: tim-mitchell

-------------- next part --------------
import hotshot, hotshot.stats
import time, os

__all__ = ['call', 'decorator', 'timeme']

_LOGFILE = 'profiler.prof'
def call(func, *args, **kwargs):
   "call(func, *args, **kwargs) --> print profile stats for func returning the result of func"
   logfile = _LOGFILE
   i = 1
   while os.path.exists(logfile):
      logfile = 'profiler%d.prof' % i
      i += 1
   prof = hotshot.Profile(logfile)
   try:
      result = prof.runcall(func, *args, **kwargs)
      prof.close()
      stats = hotshot.stats.load(logfile)
      stats.strip_dirs()
      stats.sort_stats('time', 'calls')
      stats.print_stats()
   finally:
      try:
         os.remove(logfile)
      except:
         pass
   return result

def decorator(func):
   "print profile stats for decorated function"
   def wrapper(*args, **kwargs):
      print 'Profile for:', func.__name__
      return call(func, *args, **kwargs)
   return wrapper

def timeme(func):
   "print elapsed time for decorated function"
   def wrapper(*args, **kwargs):
      t = -time.clock()
      rv = func(*args, **kwargs)
      t += time.clock()
      print func.__name__, 'took', t, 'secs'
      return rv
   return wrapper
   


More information about the wingide-users mailing list