Skip to content

Commit

Permalink
add a context manager for timing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdsharpe committed Dec 21, 2023
1 parent 62eac12 commit c1386ba
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions aerosandbox/tools/code_benchmarking.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,38 @@
import aerosandbox.numpy as np


class Timer(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 with:
with Timer("My timer") as t:
# Do stuff
print(t.runtime)
"""

def __init__(self, name=None):
self.name = name

def __enter__(self):
self.t_start = time.perf_counter_ns()

def __exit__(self, type, value, traceback):
self.runtime = (time.perf_counter_ns() - self.t_start) / 1e9
if self.name:
print(f'[{self.name}]')
print(f'Elapsed: {runtime} sec')


def time_function(
func: Callable,
repeats: int = None,
desired_runtime: float = None,
runtime_reduction = np.min,
runtime_reduction=np.min,
) -> Tuple[float, Any]:
"""
Runs a given callable and tells you how long it took to run, in seconds. Also returns the result of the function
Expand Down Expand Up @@ -70,9 +97,11 @@ def time_function_once():
result
)


if __name__ == '__main__':

def f():
time.sleep(0.1)

print(time_function(f, desired_runtime=1))

print(time_function(f, desired_runtime=1))

0 comments on commit c1386ba

Please sign in to comment.