Development/Python

Python Profiling

IMCOMKING 2020. 10. 21. 04:29




Line Profiler

python script를 한줄 단위로 profiling해줌

아래의 code snippets으로 쉽게 decorator로 구현 가능



cProfile

import cProfile, pstats, io
from pstats import SortKey
pr = cProfile.Profile()
pr.enable()
self.train_run()
pr.disable()

s = io.StringIO()
sortby = SortKey.CUMULATIVE
ps = pstats.Stats(pr
, stream=s).sort_stats(sortby)
ps.print_stats()
with open('profile.txt', 'w+') as f: f.write(s.getvalue())
print(s.getvalue())


python -m cProfile ../run_service.py > profile_result.txt


https://stackoverflow.com/questions/582336/how-can-you-profile-a-python-script


https://stackoverflow.com/questions/51536411/saving-cprofile-results-to-readable-external-file