检测时间消耗
python -m cProfile -s cumulative cprofile_test.py
-s cumulative 可按时间进行排序
[root@k8s-node-1 alex]# python -m cProfile -s cumulative cprofile_test.py
Benchmark duration: 15.244712114334106 seconds
23139965 function calls in 15.245 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 15.245 15.245 cprofile_test.py:7(<module>)
1 0.150 0.150 15.245 15.245 cprofile_test.py:67(benchmark)
40 11.086 0.277 15.095 0.377 cprofile_test.py:11(primes)
40 2.897 0.072 2.897 0.072 {range}
23139840 1.112 0.000 1.112 0.000 {method 'append' of 'list' objects}
40 0.000 0.000 0.000 0.000 {len}
2 0.000 0.000 0.000 0.000 {time.time}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
参数说明:(以primes为例)
- Benchmark duration: 15.244712114334106 seconds 输出结果
- 23139965 function calls in 15.245 seconds
- 23139965 此调用,共花费15.245 seconds
- ncalls : 函数执行次数 primes执行了40次
- cumtime (cumulative time):累积时间,包括子函数。这里说的子函数,就是在函数中调用其它的函数。primes用了15.095秒。
- tottime : 不包含子函数的时间,即primes用了11.086秒,不包括函数里面的子函数(range,append)的时间
- 11.086 + (2.897 + 1.112) = 15.095 ,此时间最为重要
- filename:lineno(function) 文件名,列数,方法名