본문 바로가기
IT 개인학습/Python

Python에서 시간 측정하기(Decorator, Command line)

by kiimy 2021. 9. 26.
728x90

* Decorator 활용

import time

from functools import wraps

def check_time(function):
	@wraps(function)
	def measure(*args, **kwargs):
		start_time = time.time()

		result = function(*args, **kwargs)

		end_time = time.time()

		print(f"@check_time: {function.__name__} took {end_time - start_time}")
		return result

	return measure
@check_time
def test_function():
	for _ in range(10000):
		print("Just print something")

if __name__ == "__main__":
	test_function()
------------------------------출력------------------------
$ python3.6 test.py
Just print something
Just print something
Just print something
Just print something
Just print something
...
@check_time: test_function took 0.03430891036987305

* Command line에서 활용

------------------------------출력Command line------------------------
$ time python test.py

real	0m0.100s
user	0m0.032s
sys 	0m0.043s
728x90

'IT 개인학습 > Python' 카테고리의 다른 글

힙 Heap 자료구조  (0) 2022.08.31
Python 내장함수 및 라이브러리  (0) 2022.05.07
Error, try ... except  (0) 2021.07.18
Class 상속(inheritance)  (0) 2021.07.18
매직메소드  (0) 2021.07.18

댓글