[python 알고리즘] 알고리즘 시간 측정
2021. 10. 28. 19:21ㆍAlgorithm
728x90
# CheckTime.py
from timeit import default_timer
from datetime import timedelta
class CheckTime:
def __init__(self, func):
self.func = func
def __call__(self, *args, **kwargs):
def wrapper_fn():
start = default_timer()
result = self.func(*args, **kwargs)
end = default_timer()
print(f"[{self.func.__name__}]: {timedelta(seconds=end - start)} sec\nresult : {result}")
return result
return wrapper_fn()
아래와 같이 측정할 메서드에 @데코레이터를 작성하면 자동으로 알고리즘 동작에 걸린 시간과 리턴값을 출력해줌.
from CheckTime import CheckTime
@CheckTime
def solution1(data):
...
@CheckTime
def solution2(data):
...
if __name__ == "__main__":
solution1(input)
solution2(input)
728x90