今天爱分享给大家带来如何测试一个python脚本的性能【面试题详解】,希望能够帮助到大家。
引入
import cProfile
cProfile.run('foo()')
执行脚本
python -m cProfile myscript.py
结果
1007 function calls in 0.061 CPU seconds
Ordered by: standard name
ncalls tottime percall 喂饭time percall filename:lineno(function)
1 0.000 0.000 0.061 0.061 :1()
1000 0.051 0.000 0.051 0.000 euler048.py:2()
1 0.005 0.005 0.061 0.061 euler048.py:2()
1 0.000 0.000 0.061 0.061 {execfile}
1 0.002 0.002 0.053 0.053 {map}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler objects}
1 0.000 0.000 0.000 0.000 {range}
1 0.003 0.003 0.003 0.003 {sum}
原文链接:http://blog.itblood.com/4543.html,转载请注明出处。

![顽皮的任务1.0汉化版 [欧美SLG/汉化/动态]](/wp-content/uploads/replace/2025/07/18/a08094d4e8d8a6cbb653e14a10a43459.webp)