Simple profiling tool#


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


sage: def f():                        # not tested
....:     p = Profiler()              # not tested

Calling p(message) creates a checkpoint:

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

Do something time-consuming:

sage: x = factor(15)                  # not tested

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

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

This will give a nice list of timings between checkpoints:

sage: print(p)                        # not tested

Let’s try it out:

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

See also



  • 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


  • David Harvey (August 2006)


Prints the last profiler step