time_memory_benchmark#

pybear.utilities.time_memory_benchmark(*args, number_of_trials=7, rest_time=1, verbose=1)#

Measure the average time (seconds) and the average change in system RAM (MB) when computing functions.

Displays statistics to the screen and returns an np.ndarray containing the raw measurements.

Parameters:
*args

Tuples of (‘function_name’, function, ARGS_AS_LIST, KWARGS_AS_DICT).

number_of_trialsint

Number of times to run each given function. Given, for example, two trials with functions f1, f2, and f3, runs are ordered as f1, f2, f3, f1, f2, f3, not f1, f1, f2, f2, f3, f3.

rest_timenumbers.Real

Time to rest in seconds before and after running a function to allow for RAM equilibration. The rest time is not included in the reported computation time.

verbosenumbers.Real

Print (verbose > 0) or do not print (verbose=0) information to the screen during run time.

Returns:
TIME_MEM_HOLDERndarray of shape (2, n_functions, n_trials)

Raw measurements of time (sec) and memory change (MB). Index 0 of the first axis contains time results, index 1 contains memory results.

Examples

>>> from pybear.utilities import time_memory_benchmark
>>> def function_a(a, b, c=1):
...     time.sleep(a + b + c)
...     return a + b + c
...
>>> def function_b(d, e, f=2):
...     time.sleep(d + e + f)
...     return d + e + f
...
>>> results = time_memory_benchmark(
...     ('function_a', function_a, [1, 2], {'c': 3}),
...     ('function_b', function_b, [0, 3], {'f': 1}),
...     number_of_trials=2,
...     rest_time=1,
...     verbose=1
... )
********************************************************************
Running trial 1...
     function_a...
     function_b...
********************************************************************
Running trial 2...
     function_a...
     function_b...

Done.

function_a time = 6.005 +/- 0.000 sec; mem = 0.000 +/- 0.000 MB

function_b time = 4.004 +/- 0.002 sec; mem = 0.000 +/- 0.000 MB

>>> print(results)
[[[6.0045845 6.005382300000001]

[4.0055356 4.0022901]]

[[0.0 0.0]

[0.0 0.0]]]