Simple profiling tool - Utilities

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

# Simple profiling tool

AUTHORS:

- David Harvey (August 2006)

- Martin Albrecht

class sage.misc.profiler.Profiler(systems=[], verbose=False)

  Bases: `object`

  Keeps track of CPU time used between a series of user-defined checkpoints.

  It’s probably not a good idea to use this class in an inner loop :-)

  EXAMPLES:

      from sage.misc.profiler import Profiler
      sage: def f():                        # not tested
      ....:     p = Profiler()

  Calling `p(message)` creates a checkpoint:

  Sage

      sage: p("try factoring 15")           # not tested

  Python

      >>> from sage.all import *
      >>> p("try factoring 15")           # not tested

  Do something time-consuming:

  Sage

      sage: x = factor(15)                  # not tested

  [Python]

      >>> from sage.all import *
      >>> x = factor(Integer(15))                  # not tested

  You can create a checkpoints without a string; `Profiler` will use the source code instead:

  Sage

      sage: # not tested
      sage: p()
      sage: y = factor(25)
      sage: p("last step")
      sage: z = factor(35)
      sage: p()

  Python

      >>> from sage.all import *
      >>> # not tested
      >>> p()
      >>> y = factor(Integer(25))
      >>> p("last step")
      >>> z = factor(Integer(35))
      >>> p()

  This will give a nice list of timings between checkpoints:

  Sage

      sage: print(p)                        # not tested

  [Python]

      >>> from sage.all import *
      >>> print(p)                        # not tested

  Let’s try it out:

  Sage

      sage: f()                             # not tested
          3.020s -- try factoring 15
         15.240s -- line 17: y = factor(25)
       5000.190s -- last step

  Python

      >>> from sage.all import *
      >>> f()                             # not tested
          3.020s -- try factoring 15
         15.240s -- line 17: y = factor(25)
       5000.190s -- last step

  See also

  `runsnake()`

  Todo

  - Add Pyrex source code inspection (I assume it doesn’t currently do this)

  - Add ability to sort output by time

  - Add option to constructor to print timing immediately when checkpoint is reached

  - Migrate to Pyrex?

  - Add ability to return timings in a more machine-friendly format

  AUTHOR:

  - David Harvey (August 2006)

  clear()
  print_last()

    Print the last profiler step.