def run(statement, filename=None, sort=-1): """Run statement under profiler optionally saving results in filename
This function takes a single argument that can be passed to the "exec" statement, and an optional file name. In all cases this routine attempts to "exec" its first argument and gather profiling statistics from the execution. If no file name is present, then this function automatically prints a simple profiling report, sorted by the standard name string (file/line/function-name) that is presented in each line. """ prof = Profile() result = None try: try: prof = prof.run(statement) except SystemExit: pass finally: if filename is not None: prof.dump_stats(filename) else: result = prof.print_stats(sort) return result
def runctx(statement, globals, locals, filename=None, sort=-1): """Run statement under profiler, supplying your own globals and locals, optionally saving results in filename.
statement and filename have the same semantics as profile.run """ prof = Profile() result = None try: try: prof = prof.runctx(statement, globals, locals) except SystemExit: pass finally: if filename is not None: prof.dump_stats(filename) else: result = prof.print_stats(sort) return result
# Backwards compatibility. def help(): print "Documentation for the profile/cProfile modules can be found " print "in the Python Library Reference, section 'The Python Profiler'."
class Profile(_lsprof.Profiler): """Profile(timer=None, timeunit=None, subcalls=True, builtins=True)
Builds a profiler object using the specified timer function. The default timer is a fast built-in one based on real time. For custom timer functions returning integers, timeunit can be a float specifying a scale (i.e. how long each integer unit is, in seconds). """
# Most of the functionality is in the base class. # This subclass only adds convenient and backward-compatible methods.
# This method is more useful to profile a single function call. def runcall(self, func, *args, **kw): self.enable() try: return func(*args, **kw) finally: self.disable()