Skip to content

Commit

Permalink
Merge pull request #105 from jusschwitalla/TAQ-169_timer_decorator
Browse files Browse the repository at this point in the history
timer decorator
  • Loading branch information
Marvmann authored Jan 30, 2024
2 parents 3f1d702 + b4a4e5a commit 3be6f38
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import subprocess
import sys
import time
from typing import Union
from typing import Union, Callable

import inquirer

Expand Down Expand Up @@ -202,3 +202,39 @@ def end_time_measurement(start: float) -> float:
"""
end = time.perf_counter()
return round((end - start) * 1000, 3)


def stop_watch(position: int = None) -> Callable:
"""
Usage as decorator to measure time, eg:
```
@stop_watch()
def run(input_data,...):
return processed_data
```
results in valid:
```
processed_data, time_to_process = run(input,...)
```
If the return value of the decorated function is of type tuple,
the optional parameter `position` can be used to specify the position at which the
measured time is to be inserted in the return tuple.
:param position: The position at which the measured time gets inserted in the return tuple.
If not specified the measured time will be appended to the original return value.
:type position: int
:return: The wrapper function
:rtype: Callable
"""
def wrap(func):
def wrapper(*args, **kwargs):
start = start_time_measurement()
return_value = func(*args, **kwargs)
duration = end_time_measurement(start)
if position is not None and isinstance(return_value, tuple):
rv = list(return_value)
rv.insert(position, duration)
return tuple(rv)
return return_value, duration
return wrapper
return wrap

0 comments on commit 3be6f38

Please sign in to comment.