使用Kernprof更改时间单位

Foo*_*Bar 6 python profiling line-profiler

我开始使用来寻找Python的瓶颈line_profiler。现在,我正在通过运行

kernprof -l -v myFile.py
Run Code Online (Sandbox Code Playgroud)

但是,时间单位似乎为1e-6,导致输出结果如132329040。如何增加时间间隔,以使输出在更大的时间增量内更具可读性?

Pep*_*ino 9

这是迄今为止仅通过 Jupyter line magic 提供的功能,如此处所述。可以通过“-u”标志访问它,后跟计时器单位(以秒为单位)。这是一个示例用法:

def m():
  return [0]*10**8

%lprun -u 1e-3 -f m m()
Run Code Online (Sandbox Code Playgroud)

它以毫秒为单位显示计时器的输出:

Out:
Timer unit: 0.001 s

Total time: 0.363548 s
File: 
Function: m at line 1

Line # Hits Time Per Hit % Time Line Contents
==============================================================
1 def m():
2 1 363.5 363.5 100.0 return [0]*10**8
Run Code Online (Sandbox Code Playgroud)

如本PR中所述。


小智 6

我通过阅读 LineProfiler 源代码找到了解决方案。您可以使用以下参数“output_unit”来更改时间单位。

profiler.print_stats(output_unit=1e-03)
Run Code Online (Sandbox Code Playgroud)

output_unit=1e-03,控制台将输出“定时器单位:0.001 s” output_unit=1,控制台将输出“定时器单位:1 s”


小智 -2

使用“ms”显示:

编辑line_profiler.py -> show_func -> 找到for lineno, nhits, time in timings: 这一行 -> 更改timetime * 10-03