C Function Profiler Using Google Perftools - Utilities

Skip to content
Back to top
View this page
Edit this page

# 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

    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 = ...

Python

    >>> 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:

This 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)

  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

      sage: from sage.misc.gperftools import Profiler
      sage: Profiler()
      Profiler logging to ...

  Python

      >>> from sage.all import *
      >>> from sage.misc.gperftools import Profiler
      >>> Profiler()
      Profiler logging to ...

  filename()

    Return the file name.

    OUTPUT: string

    EXAMPLES:

    Sage

        sage: from sage.misc.gperftools import Profiler
        sage: prof = Profiler()
        sage: prof.filename()
        '.../tmp_....perf'

    Python

        >>> from sage.all import *
        >>> from sage.misc.gperftools import Profiler
        >>> prof = Profiler()
        >>> prof.filename()
        '.../tmp_....perf'

  save(filename, cumulative=True, verbose=True)

    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

        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

    Python

        >>> 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()

    Start profiling.

    EXAMPLES:

    Sage

        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 = ...

    Python

        >>> 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()

    Stop the CPU profiler.

    EXAMPLES:

    Sage

        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 = ...

    Python

        >>> 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)

    Print text report.

    OUTPUT: nothing; a textual report is printed to stdout

    EXAMPLES:

    Sage

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

    Python

        >>> 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)

  Profile single statement.

  - `s` – string; Sage code to profile

  - `evaluator` – callable to evaluate

  EXAMPLES:

  Sage

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

  Python

      >>> 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()

  Used for doctesting.

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

  EXAMPLES:

  Sage

      sage: from sage.misc.gperftools import run_100ms
      sage: run_100ms()

  Python

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