checkpointing.util.timing
Utilities for timing a function.
1""" 2Utilities for timing a function. 3""" 4 5import time 6from typing import Callable, Tuple, Dict, TypeVar 7 8 9class Timer: 10 """ 11 Keep track of the interval between two time points. 12 """ 13 def __init__(self) -> None: 14 self.__start = None 15 16 def start(self): 17 """ 18 Record the start time point. 19 """ 20 self.__start = time.time() 21 return self 22 23 @property 24 def time(self): 25 """ 26 The total seconds between the latest `timer.start()` call and now. 27 """ 28 if self.__start is None: 29 raise RuntimeError("Timer.start() has never been called") 30 31 return time.time() - self.__start 32 33ReturnValue = TypeVar("ReturnValue") 34 35def timed_run(func: Callable[..., ReturnValue], *args: Tuple, **kwargs: Dict) -> Tuple[ReturnValue, float]: 36 """ 37 Run the function with the arguments, recording the run time. 38 39 Returns: 40 Tuple of two elements: 41 - Return value of the function call 42 - Time it takes to run the function 43 """ 44 t = Timer().start() 45 res = func(*args, **kwargs) 46 return res, t.time
class
Timer:
10class Timer: 11 """ 12 Keep track of the interval between two time points. 13 """ 14 def __init__(self) -> None: 15 self.__start = None 16 17 def start(self): 18 """ 19 Record the start time point. 20 """ 21 self.__start = time.time() 22 return self 23 24 @property 25 def time(self): 26 """ 27 The total seconds between the latest `timer.start()` call and now. 28 """ 29 if self.__start is None: 30 raise RuntimeError("Timer.start() has never been called") 31 32 return time.time() - self.__start
Keep track of the interval between two time points.
def
timed_run( func: Callable[..., ~ReturnValue], *args: Tuple, **kwargs: Dict) -> Tuple[~ReturnValue, float]:
36def timed_run(func: Callable[..., ReturnValue], *args: Tuple, **kwargs: Dict) -> Tuple[ReturnValue, float]: 37 """ 38 Run the function with the arguments, recording the run time. 39 40 Returns: 41 Tuple of two elements: 42 - Return value of the function call 43 - Time it takes to run the function 44 """ 45 t = Timer().start() 46 res = func(*args, **kwargs) 47 return res, t.time
Run the function with the arguments, recording the run time.
Returns
Tuple of two elements:
- Return value of the function call
- Time it takes to run the function