-
functools.cachepython/응용 2023. 7. 13. 19:05
코드
import contextlib import functools import time @contextlib.contextmanager def timer(): start = time.time() yield end = time.time() elapsed = round(end - start, 3) print(f"{elapsed=}s") @functools.cache def sleep(n): time.sleep(n) with timer(): sleep(2) with timer(): sleep(2)
결과
elapsed=2.004s elapsed=0.0s
참고
'python > 응용' 카테고리의 다른 글
Generators in python (0) 2023.08.20 Pydantic 으로 dataclass 대체하기 (0) 2023.07.29 pydantic: dictionary 를 object 로 변환 / json 출력 (0) 2023.07.10 dataclass: sort (0) 2023.07.10 dataclass: mutable default values (0) 2023.07.09