使用第三方工具line_profiler
python3.6
1、安装
直接使用pip install line_profiler报错:
ERROR: Failed building wheel for line-profiler
Failed to build line-profiler
ERROR: Could not build wheels for line-profiler which use PEP 517 and cannot be installed directly
使用方法
pip3 install Cpython
pip3 install Cython git+https://github.com/rkern/line_profiler.git
但是安装Cpython没报错,运行第二行命令却报错:ModuleNotFoundError: No module named ‘cython’
解决办法,pip3 install --upgrade cython
最终安装命令顺序如下:
1)、pip3 install Cpython
2)、pip3 install --upgrade cython
3)、pip3 install Cython git+https://github.com/rkern/line_profiler.git
2、使用方法
prof = line_profiler.LineProfiler(函数名)
prof.enable()
函数调用
prof.disable()
prof.print_stats(sys.stdout)
3、输出
Timer unit: 3.41307e-07 s :表示单位时间
Total time: 0.792417 s :表示运行总时间
Line # :表示在代码行数
Hits :表示该行代码运行次数
Time :表示运行该行运行总时间
Per Hit :表示该行每次运行需要时间
% Time :表示该行耗时占总耗时百分比
Line Contents :表示代码内容