| | 1 | = Pythonメモ |
| | 2 | |
| | 3 | == プロファイラ |
| | 4 | |
| | 5 | {{{ |
| | 6 | $ python -m cProfile -o test.prof test.py |
| | 7 | }}} |
| | 8 | |
| | 9 | サーバプロセスの場合は、SIGINTで停止するとプロファイル情報が出力される。 |
| | 10 | |
| | 11 | {{{ |
| | 12 | $ kill -s SIGINT <pid> |
| | 13 | }}} |
| | 14 | |
| | 15 | 下記のスクリプトでプロファイル情報を表示することができる。 |
| | 16 | |
| | 17 | {{{ |
| | 18 | import pstats,sys |
| | 19 | p = pstats.Stats(sys.argv[1]) |
| | 20 | p.strip_dirs().sort_stats('cumtime').print_stats(); |
| | 21 | }}} |
| | 22 | |
| | 23 | {{{ |
| | 24 | $ python pstats.py test.prof |
| | 25 | Sat Mar 17 03:27:35 2018 test.prof |
| | 26 | |
| | 27 | 53176950 function calls (6 primitive calls) in 41.878 seconds |
| | 28 | |
| | 29 | Ordered by: cumulative time |
| | 30 | |
| | 31 | ncalls tottime percall cumtime percall filename:lineno(function) |
| | 32 | 1 0.000 0.000 41.878 41.878 test.py:1(<module>) |
| | 33 | 53176946/2 21.869 0.000 21.869 10.934 test.py:3(fib) |
| | 34 | 1 0.000 0.000 20.009 20.009 test.py:10(slee) |
| | 35 | 1 20.009 20.009 20.009 20.009 {time.sleep} |
| | 36 | 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} |
| | 37 | }}} |
| | 38 | |
| | 39 | tottime: 関数の実実行時間(ネストした関数の実行時間ははいらない) |
| | 40 | cumtime: 関数の実行時間 |