Note

This is an old version of the documentation. See http://docs.astropy.org/en/stable for the latest version.

timefunc

astropy.utils.timer.timefunc(num_tries=1, verbose=True)[source] [edit on github]

Decorator to time a function or method.

Parameters:

num_tries : int, optional

Number of calls to make. Timer will take the average run time.

verbose : bool, optional

Extra log information.

Returns:

tt : float

Average run time in seconds.

result

Output(s) from the function.

Examples

To add timer to time numpy.log for 100 times with verbose output:

import numpy as np
from astropy.utils.timer import timefunc

@timefunc(100)
def timed_log(x):
    return np.log(x)

To run the decorated function above:

>>> t, y = timed_log(100)
INFO: timed_log took 9.29832458496e-06 s on AVERAGE for 100 call(s). [...]
>>> t
9.298324584960938e-06
>>> y
4.6051701859880918