mirror of https://gitlab.com/pamhyr/pamhyr2
tools: Add timer function wrapped.
parent
fcc31f22e7
commit
1e19ecbb31
|
|
@ -8,10 +8,15 @@ from PyQt5.QtCore import QTranslator
|
||||||
from PyQt5.QtWidgets import QApplication
|
from PyQt5.QtWidgets import QApplication
|
||||||
|
|
||||||
from config import Config
|
from config import Config
|
||||||
|
from tools import (
|
||||||
|
reset_timers, display_timers, timer
|
||||||
|
)
|
||||||
|
|
||||||
from View.MainWindow import ApplicationWindow
|
from View.MainWindow import ApplicationWindow
|
||||||
from Model.Study import Study
|
from Model.Study import Study
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
reset_timers()
|
||||||
conf = Config.load()
|
conf = Config.load()
|
||||||
app = QApplication(sys.argv)
|
app = QApplication(sys.argv)
|
||||||
|
|
||||||
|
|
@ -39,7 +44,10 @@ def main():
|
||||||
|
|
||||||
application = ApplicationWindow(conf=conf)
|
application = ApplicationWindow(conf=conf)
|
||||||
application.show()
|
application.show()
|
||||||
sys.exit(app.exec_())
|
|
||||||
|
ret = app.exec_()
|
||||||
|
display_timers()
|
||||||
|
sys.exit(ret)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
|
||||||
53
src/tools.py
53
src/tools.py
|
|
@ -1,9 +1,60 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import time
|
||||||
from functools import (
|
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):
|
def flatten(lst):
|
||||||
"""Flatten list of list
|
"""Flatten list of list
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue