### Navigation

- index
- modules |
- next |
- previous |
- Miscellaneous »

# 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` evironment 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 = ...

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)

  Bases: `sage.structure.sage_object.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 ...

  `filename`()

    Return the file name

    OUTPUT:

    String.

    EXAMPLES:

        sage: from sage.misc.gperftools import Profiler
        sage: prof = Profiler()
        sage: 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 (optional, default: `True`). Whether to return cumulative timings.
    - `verbose` – boolean (optional, 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

  `start`()

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

  `stop`()

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

  `top`(cumulative=True)

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

`sage.misc.gperftools.``crun`(s, evaluator)

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

`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: from sage.misc.gperftools import run_100ms
      sage: run_100ms()

### Navigation

- index
- modules |
- next |
- previous |
- Miscellaneous »