Last modified 8 years ago
Pythonメモ
プロファイラ
$ python -m cProfile -o test.prof test.py
サーバプロセスの場合は、SIGINTで停止するとプロファイル情報が出力される。
$ kill -s SIGINT <pid>
下記のスクリプトでプロファイル情報を表示することができる。
import pstats,sys
p = pstats.Stats(sys.argv[1])
p.strip_dirs().sort_stats('cumtime').print_stats();
$ python pstats.py test.prof
Sat Mar 17 03:27:35 2018 test.prof
53176950 function calls (6 primitive calls) in 41.878 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 41.878 41.878 test.py:1(<module>)
53176946/2 21.869 0.000 21.869 10.934 test.py:3(fib)
1 0.000 0.000 20.009 20.009 test.py:10(slee)
1 20.009 20.009 20.009 20.009 {time.sleep}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
tottime: 関数の実実行時間(ネストした関数の実行時間ははいらない) cumtime: 関数の実行時間
