Back to index

python3.2  3.2.2
Classes | Functions | Variables
cProfile Namespace Reference

Classes

class  Profile

Functions

def run
def runctx
def label
def main

Variables

list __all__ = ["run", "runctx", "Profile"]

Function Documentation

def cProfile.label (   code)

Definition at line 150 of file cProfile.py.

00150 
00151 def label(code):
00152     if isinstance(code, str):
00153         return ('~', 0, code)    # built-in functions ('~' sorts at the end)
00154     else:
00155         return (code.co_filename, code.co_firstlineno, code.co_name)
00156 
00157 # ____________________________________________________________

def cProfile.main ( void  )

Definition at line 158 of file cProfile.py.

00158 
00159 def main():
00160     import os, sys
00161     from optparse import OptionParser
00162     usage = "cProfile.py [-o output_file_path] [-s sort] scriptfile [arg] ..."
00163     parser = OptionParser(usage=usage)
00164     parser.allow_interspersed_args = False
00165     parser.add_option('-o', '--outfile', dest="outfile",
00166         help="Save stats to <outfile>", default=None)
00167     parser.add_option('-s', '--sort', dest="sort",
00168         help="Sort order when printing to stdout, based on pstats.Stats class",
00169         default=-1)
00170 
00171     if not sys.argv[1:]:
00172         parser.print_usage()
00173         sys.exit(2)
00174 
00175     (options, args) = parser.parse_args()
00176     sys.argv[:] = args
00177 
00178     if len(args) > 0:
00179         progname = args[0]
00180         sys.path.insert(0, os.path.dirname(progname))
00181         with open(progname, 'rb') as fp:
00182             code = compile(fp.read(), progname, 'exec')
00183         globs = {
00184             '__file__': progname,
00185             '__name__': '__main__',
00186             '__package__': None,
00187             '__cached__': None,
00188         }
00189         runctx(code, globs, None, options.outfile, options.sort)
00190     else:
00191         parser.print_usage()
00192     return parser
00193 
# When invoked as main program, invoke the profiler on a script

Here is the call graph for this function:

def cProfile.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.

Definition at line 14 of file cProfile.py.

00014 
00015 def run(statement, filename=None, sort=-1):
00016     """Run statement under profiler optionally saving results in filename
00017 
00018     This function takes a single argument that can be passed to the
00019     "exec" statement, and an optional file name.  In all cases this
00020     routine attempts to "exec" its first argument and gather profiling
00021     statistics from the execution. If no file name is present, then this
00022     function automatically prints a simple profiling report, sorted by the
00023     standard name string (file/line/function-name) that is presented in
00024     each line.
00025     """
00026     prof = Profile()
00027     result = None
00028     try:
00029         try:
00030             prof = prof.run(statement)
00031         except SystemExit:
00032             pass
00033     finally:
00034         if filename is not None:
00035             prof.dump_stats(filename)
00036         else:
00037             result = prof.print_stats(sort)
00038     return result

Here is the caller graph for this function:

def cProfile.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

Definition at line 39 of file cProfile.py.

00039 
00040 def runctx(statement, globals, locals, filename=None, sort=-1):
00041     """Run statement under profiler, supplying your own globals and locals,
00042     optionally saving results in filename.
00043 
00044     statement and filename have the same semantics as profile.run
00045     """
00046     prof = Profile()
00047     result = None
00048     try:
00049         try:
00050             prof = prof.runctx(statement, globals, locals)
00051         except SystemExit:
00052             pass
00053     finally:
00054         if filename is not None:
00055             prof.dump_stats(filename)
00056         else:
00057             result = prof.print_stats(sort)
00058     return result
00059 
00060 # ____________________________________________________________

Here is the caller graph for this function:


Variable Documentation

list cProfile.__all__ = ["run", "runctx", "Profile"]

Definition at line 7 of file cProfile.py.