ttsserver/analytics.py
2024-05-29 02:39:47 +03:00

39 lines
894 B
Python

import time
def measure(function: callable, name_to_display: str = ''):
"""
Decorator to measure function (method) execution time
Use as easy as
@utils.measure
def im_function_to_measure():
....
:param name_to_display:
:param function:
:return:
"""
if name_to_display != '':
name_to_display = name_to_display + ':'
def decorated(*args, **kwargs):
start = time.time()
result = function(*args, **kwargs)
end = time.time()
print(f'{name_to_display}{function.__name__}:{args[1:]} {(end - start) * 100} ms')
return result
return decorated
class Measure:
def __init__(self, name: str):
self.start = time.time()
self.name = name
def record(self) -> None:
print(f'{self.name}: {(time.time() - self.start) * 100} ms')