使用line_profiler模块 1)首先安装line_profiler pip install line_profiler 2)在需要测试的函数前添加装饰器 @profile 3)启动程序 $kernprof -l -v test.py