tools: Add timer function wrapped.

mesh
Pierre-Antoine Rouby 2023-04-18 12:04:20 +02:00
parent fcc31f22e7
commit 1e19ecbb31
2 changed files with 61 additions and 2 deletions

View File

@ -8,10 +8,15 @@ from PyQt5.QtCore import QTranslator
from PyQt5.QtWidgets import QApplication
from config import Config
from tools import (
reset_timers, display_timers, timer
)
from View.MainWindow import ApplicationWindow
from Model.Study import Study
def main():
reset_timers()
conf = Config.load()
app = QApplication(sys.argv)
@ -39,7 +44,10 @@ def main():
application = ApplicationWindow(conf=conf)
application.show()
sys.exit(app.exec_())
ret = app.exec_()
display_timers()
sys.exit(ret)
if __name__ == "__main__":
main()

View File

@ -1,9 +1,60 @@
# -*- coding: utf-8 -*-
import time
from functools import (
reduce, partial
reduce, partial, wraps
)
##########
# TIMERS #
##########
_timers = {}
_calls = {}
def reset_timers():
global _timers
global _calls
_timers = {}
_calls = {}
def display_timers():
global _timers
global _calls
print(" +--Timers----------------------------------------+")
for func in _timers:
print(f" | {func:<15} | {_timers[func]:>10.6f} sec | {_calls[func]:>5} calls |")
print(" +------------------------------------------------+")
def timer(func):
"""Function wrapper to register function runtime"""
@wraps(func)
def wrapper(*args, **kwargs):
start_time = time.perf_counter()
value = func(*args, **kwargs)
end_time = time.perf_counter()
run_time = end_time - start_time
if func.__name__ not in _timers:
_timers[func.__name__] = 0
_calls[func.__name__] = 0
_timers[func.__name__] += run_time
_calls[func.__name__] += 1
return value
return wrapper
################
# OTHERS TOOLS #
################
@timer
def flatten(lst):
"""Flatten list of list