性能分析

cProfile

用法

想评测test.py里面的 main() 函数:

import cProfile
import pstats
from pstats import SortKey
import test

cProfile.run('test.main()','restats')
p = pstats.Stats('restats')
p.strip_dirs().sort_stats(-1).print_stats()
p.sort_stats(SortKey.CUMULATIVE).print_stats(10)
名称 含义
ncalls 调用次数
tottime 在给定函数中花费的总时间(不包括调用子函数的时间)
percall tottime除以ncalls的商
cumtime 是在这个函数和所有子函数中花费的累积时间(从调用到退出)。
percall 是cumtime除以原始调用次数的商
filename:lineno(function) 提供每个函数的各自信息

样例

性能分析.png

perf

如果无法安装可以更新一下kernel version

sudo apt-get search xxxxxx