- 1. time.process_time() 이용
- 2. timeit 이용
1. time.prcess_time()
시간측정 단위는 초(second)입니다.
import time
from datetime import timedelta
start = time.process_time()
# 본인이 시간을 측정하고 싶은 코드를 집어넣는 곳
ans = 0
for _ in range(1000000):
ans += 1
end = time.process_time()
print("seconds: ", end - start)
print("h:m:s : ", timedelta(seconds=end-start))
start 변수에 시간 측정 시작점을 저장해준 뒤 end 변수에 시간 측정 종료지점을 저장해줍니다.
그 이후에는 본인이 보기 편한 서식으로 시간을 출력해주면 됩니다.
end- start : 소요된 시간이 초 단위로 출력되는 서식timedelta(second=end-start): 소요된 시간이 시:분:초 형태로 출력됩니다.
출력결과
seconds: 0.125
h:m:s : 0:00:00.125000
2. timeit
시간측정 단위는 초(second)입니다.
from timeit import default_timer as timer
from datetime import timedelta
start = timer()
# 본인이 시간을 측정하고 싶은 코드를 집어넣는 곳
ans = 0
for _ in range(1000000):
ans += 1
end = timer()
print("seconds: ", end - start)
print("h:m:s : ", timedelta(seconds=end-start))
이전에 실행했던 time.process_time() 사용법과 같습니다. 다만 default_timer 라이브러리에서 imort 하는 것이 차이점입니다.
출력결과
seconds: 0.1577229000395164
h:m:s : 0:00:00.157723
참고자료
https://realpython.com/python-time-module/
A Beginner’s Guide to the Python time Module – Real Python
In this tutorial, you'll learn how to use the Python time module to represent dates and times in your application, manage code execution, and measure performance.
realpython.com
'Dev Lang > Python' 카테고리의 다른 글
[Python] else의 다른 활용 (0) | 2023.08.11 |
---|---|
[Python] reverse, reversed (0) | 2023.05.21 |
[Python] 순열(Permutation) 구현 (0) | 2022.08.09 |
[Python] 중복되는 데이터를 제거하고 싶을 경우 (0) | 2022.07.18 |
[Python] 넘파이 numpy (0) | 2022.03.02 |