C Function Profiler Using Google Perftools

Note that the profiler samples 100x per second by default. In particular, you cannot profile anything shorter than 10ms. You can adjust the rate with the CPUPROFILE_FREQUENCY environment variable if you want to change it.

EXAMPLES:

sage: from sage.misc.gperftools import Profiler, run_100ms
sage: prof = Profiler()
sage: prof.start()       # optional - gperftools
sage: run_100ms()
sage: prof.stop()        # optional - gperftools
PROFILE: interrupts/evictions/bytes = ...
>>> from sage.all import *
>>> from sage.misc.gperftools import Profiler, run_100ms
>>> prof = Profiler()
>>> prof.start()       # optional - gperftools
>>> run_100ms()
>>> prof.stop()        # optional - gperftools
PROFILE: interrupts/evictions/bytes = ...

REFERENCE:

Uses the Google performance analysis tools. Note that they are not included in Sage, you have to install them yourself on your system.

AUTHORS:

  • Volker Braun (2014-03-31): initial version

class sage.misc.gperftools.Profiler(filename=None)[source]

Bases: SageObject

Interface to the gperftools profiler.

INPUT:

  • filename – string or None (default). The file name to log to. By default, a new temporary file is created.

EXAMPLES:

sage: from sage.misc.gperftools import Profiler
sage: Profiler()
Profiler logging to ...
>>> from sage.all import *
>>> from sage.misc.gperftools import Profiler
>>> Profiler()
Profiler logging to ...
filename()[source]

Return the file name.

OUTPUT: string

EXAMPLES:

sage: from sage.misc.gperftools import Profiler
sage: prof = Profiler()
sage: prof.filename()
'.../tmp_....perf'
>>> from sage.all import *
>>> from sage.misc.gperftools import Profiler
>>> prof = Profiler()
>>> prof.filename()
'.../tmp_....perf'
save(filename, cumulative=True, verbose=True)[source]

Save report to disk.

INPUT:

  • filename – string; the filename to save at. Must end with one of .dot, .ps, .pdf, .svg, .gif, or .txt to specify the output file format.

  • cumulative – boolean (default: True); whether to return cumulative timings

  • verbose – boolean (default: True); whether to print informational messages

EXAMPLES:

sage: from sage.misc.gperftools import Profiler, run_100ms
sage: prof = Profiler()
sage: prof.start()    # optional - gperftools
sage: run_100ms()     # optional - gperftools
sage: prof.stop()     # optional - gperftools
PROFILE: interrupts/evictions/bytes = ...
sage: f = tmp_filename(ext='.txt')      # optional - gperftools
sage: prof.save(f, verbose=False)       # optional - gperftools
>>> from sage.all import *
>>> from sage.misc.gperftools import Profiler, run_100ms
>>> prof = Profiler()
>>> prof.start()    # optional - gperftools
>>> run_100ms()     # optional - gperftools
>>> prof.stop()     # optional - gperftools
PROFILE: interrupts/evictions/bytes = ...
>>> f = tmp_filename(ext='.txt')      # optional - gperftools
>>> prof.save(f, verbose=False)       # optional - gperftools
start()[source]

Start profiling.

EXAMPLES:

sage: from sage.misc.gperftools import Profiler, run_100ms
sage: prof = Profiler()
sage: prof.start()    # optional - gperftools
sage: run_100ms()
sage: prof.stop()     # optional - gperftools
PROFILE: interrupts/evictions/bytes = ...
>>> from sage.all import *
>>> from sage.misc.gperftools import Profiler, run_100ms
>>> prof = Profiler()
>>> prof.start()    # optional - gperftools
>>> run_100ms()
>>> prof.stop()     # optional - gperftools
PROFILE: interrupts/evictions/bytes = ...
stop()[source]

Stop the CPU profiler.

EXAMPLES:

sage: from sage.misc.gperftools import Profiler, run_100ms
sage: prof = Profiler()
sage: prof.start()    # optional - gperftools
sage: run_100ms()
sage: prof.stop()     # optional - gperftools
PROFILE: interrupts/evictions/bytes = ...
>>> from sage.all import *
>>> from sage.misc.gperftools import Profiler, run_100ms
>>> prof = Profiler()
>>> prof.start()    # optional - gperftools
>>> run_100ms()
>>> prof.stop()     # optional - gperftools
PROFILE: interrupts/evictions/bytes = ...
top(cumulative=True)[source]

Print text report.

OUTPUT: nothing; a textual report is printed to stdout

EXAMPLES:

sage: from sage.misc.gperftools import Profiler
sage: prof = Profiler()
sage: prof.start()    # optional - gperftools
sage: # do something
sage: prof.stop()     # optional - gperftools
PROFILE: interrupts/evictions/bytes = ...
sage: prof.top()   # optional - gperftools
Using local file ...
Using local file ...
>>> from sage.all import *
>>> from sage.misc.gperftools import Profiler
>>> prof = Profiler()
>>> prof.start()    # optional - gperftools
>>> # do something
>>> prof.stop()     # optional - gperftools
PROFILE: interrupts/evictions/bytes = ...
>>> prof.top()   # optional - gperftools
Using local file ...
Using local file ...
sage.misc.gperftools.crun(s, evaluator)[source]

Profile single statement.

  • s – string; Sage code to profile

  • evaluator – callable to evaluate

EXAMPLES:

sage: import sage.misc.gperftools as gperf
sage: ev = lambda ex:eval(ex, globals(), locals())
sage: gperf.crun('gperf.run_100ms()', evaluator=ev)   # optional - gperftools
PROFILE: interrupts/evictions/bytes = ...
Using local file ...
Using local file ...
>>> from sage.all import *
>>> import sage.misc.gperftools as gperf
>>> ev = lambda ex:eval(ex, globals(), locals())
>>> gperf.crun('gperf.run_100ms()', evaluator=ev)   # optional - gperftools
PROFILE: interrupts/evictions/bytes = ...
Using local file ...
Using local file ...
sage.misc.gperftools.run_100ms()[source]

Used for doctesting.

A function that performs some computation for more than (but not that much more than) 100ms.

EXAMPLES:

sage: from sage.misc.gperftools import run_100ms
sage: run_100ms()
>>> from sage.all import *
>>> from sage.misc.gperftools import run_100ms
>>> run_100ms()