https://docs.python.org/3/tutorial/stdlib.html#performance-measurement
- timeit는 작은 코드 스니펫의 실행 시간을 정밀하게 측정합니다. Timer(stmt, setup).timeit()이 여러 번 반복 실행 후 평균 시간을 반환합니다.
from timeit import Timer
#전통적 스왑(t=a; a=b; b=t)보다 튜플 언패킹(a,b = b,a)이 약간 빠름을 보여줍니다.
Timer('t=a; a=b; b=t', 'a=1; b=2').timeit()
#0.57535828626024577
Timer('a,b = b,a', 'a=1; b=2').timeit()
#0.54962537085770791python -m timeit -s "data=list(range(100))"
python -m timeit -n 10000 -r 5 "a,b=b,a" -s "a=1;b=2"
실행 1만 번, 5번 반목해 결과 출력
코드 전체 흐름의 병목 → cProfile; 미세 비교/마이크로 벤치 → timeit; 터미널 즉석 확인 → python -m timeit; 결과 후처리/정렬/필터 → pstats.
- 더 큰 코드 블록의 병목을 찾으려면 profile / cProfile와 결과 분석용 pstats를 사용해 함수별 누적 시간 등을 확인할 수 있습니다.