# Simple profiling tool#

AUTHORS:

• David Harvey (August 2006)

• Martin Albrecht

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

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: p("try factoring 15")           # not tested
```
```>>> from sage.all import *
>>> p("try factoring 15")           # not tested
```

Do something time-consuming:

```sage: x = factor(15)                  # not tested
```
```>>> 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: # not tested
sage: p()
sage: y = factor(25)
sage: p("last step")
sage: z = factor(35)
sage: p()
```
```>>> 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: print(p)                        # not tested
```
```>>> from sage.all import *
>>> 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
```
```>>> from sage.all import *
>>> f()                             # not tested
3.020s -- try factoring 15
15.240s -- line 17: y = factor(25)
5000.190s -- last step
```

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()[source]#
print_last()[source]#

Prints the last profiler step