aerosandbox.tools.code_benchmarking#

Module Contents#

Classes#

Timer

A context manager for timing things. Use it like this:

Functions#

time_function(func[, repeats, desired_runtime, ...])

Runs a given callable and tells you how long it took to run, in seconds. Also returns the result of the function

f()

class aerosandbox.tools.code_benchmarking.Timer(name=None)[source]#

Bases: object

A context manager for timing things. Use it like this:

with Timer(“My timer”): # You can optionally give it a name

# Do stuff

Results are printed to stdout. You can access the runtime (in seconds) directly by instantiating the object:

>>> t = Timer("My timer")
>>> t.tic()
>>> # Do stuff
>>> print(t.toc())

Nested timers are also supported. For example, this code: >>> with Timer(“a”): >>> with Timer(“b”): >>> with Timer(“c”): >>> f()

prints the following console output:

[a] Timing…
[b] Timing…

[c] Timing… [c] Elapsed: 100 msec

[b] Elapsed: 100 msec

[a] Elapsed: 100 msec

Parameters:

name (str) –

number_running: int = 0[source]#
__repr__()[source]#

Return repr(self).

static _format_time(time_seconds)[source]#
_print(s, number_running_mod=0)[source]#
Parameters:
  • s (str) –

  • number_running_mod (int) –

tic()[source]#
__enter__()[source]#
toc()[source]#
Return type:

float

__exit__(type, value, traceback)[source]#
aerosandbox.tools.code_benchmarking.time_function(func, repeats=None, desired_runtime=None, runtime_reduction=np.min)[source]#
Runs a given callable and tells you how long it took to run, in seconds. Also returns the result of the function

(if any), for good measure.

Parameters:
  • func (Callable) – The function to run. Should take no arguments; use a lambda function or functools.partial if you need to pass arguments.

  • repeats (int) – The number of times to run the function. If None, runs until desired_runtime is met.

  • desired_runtime (float) – The desired runtime of the function, in seconds. If None, runs until repeats is met.

  • runtime_reduction – A function that takes in a list of runtimes and returns a reduced value. For example, np.min will return the minimum runtime, np.mean will return the mean runtime, etc. Default is np.min.

Return type:

Tuple[float, Any]

Returns: A Tuple of (time_taken, result).
  • time_taken is a float of the time taken to run the function, in seconds.

  • result is the result of the function, if any.

aerosandbox.tools.code_benchmarking.f()[source]#