IT 개인학습/Python

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

그때 그때 끄적 2021. 9. 26. 12:55
반응형

* 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
반응형