2017-12-19 31 views
0

안녕하세요, 아래 주어진대로 python 스크립트를 프로파일 링하는 명령 줄 방법의 사용법을 알고 있습니다. cProfile.Profile()의 출력을 Python 스크립트의 * .prof 파일에 저장하는 방법

python -m cProfile -o program.prof my_program.py

그러나 나는 아래로 cprofile 명령 모듈을 사용하여 파이썬 코드의 특정 부분을 프로파일 링하고있다. 어떻게

import cProfile, pstats, io 
pr = cProfile.Profile() 
pr.enable() 
# ... do something ... 
pr.disable() 
s = io.StringIO() 
sortby = 'cumulative' 
ps = pstats.Stats(pr, stream=s).sort_stats(sortby) 
ps.print_stats() 
print(s.getvalue()) 

나는 pr 대신 분석 및 프로파일 링 결과를 인쇄 할 pstats.Stats()를 사용하는 *.profile 파일 cProfile.Profile()의 출력을 저장합니다.

그래서 SnakeViz 또는 유사한 유틸리티를 사용하여 통계를 시각적으로 분석하는 데 사용할 수 있습니다.

답변

0

프로필 클래스는 a method to write the stats to a file입니다. 이를 사용하여 출력을 파일에 저장할 수 있습니다.

filename = 'profile.prof' # You can change this if needed 
pr.dump_stats(filename)