RFC: profile.py (was Re: Profiling problem)

Randall Hopper aa8vb at vislab.epa.gov
Mon May 10 07:02:35 EDT 1999


 |> >>> class C:
 |> ...    def slow_dog( self ):
 |> ...       import time
 |> ...       time.sleep(5)
 |> ...    def doit( self ):
 |> ...       import profile
 |> ...       profile.run( 'self.slow_dog()' )
 |> ... 
 |> >>> c = C()
 |> >>> c.doit()
 |> Traceback (innermost last):
 |[snip]
 |> NameError: self

Thanks to all who responded.  Duplicating the profile.run convenience
function and modifying it to invoke runctx() instead of run() was just what
was needed.

For future profiling convenience, we could consider adding a method to
profile.py to make this usage simple (as with run()):

   def runctx(statement, globals, locals, *args):
           prof = Profile()
           try:
                   prof = prof.runctx( statement, globals, locals )
           except SystemExit:
                   pass
           if args:
                   prof.dump_stats(args[0])
           else:
                   return prof.print_stats()

Thanks,

Randall




More information about the Python-list mailing list