23

我试图弄清楚如何运行 Python 的line_profiler以获取该问题答案中给出的格式的逐行执行时间。

我安装了模块并LineProfiler如下调用它的对象,但我得到的输出只是一次,而不是逐行摘要。

有任何想法吗?此外,我怎样才能获得numbers = [random.randint(1,100) for i in range(1000)]任何功能之外的线路的时间?

from line_profiler import LineProfiler
import random

def do_stuff(numbers):

    s = sum(numbers)
    l = [numbers[i]/43 for i in range(len(numbers))]
    m = ['hello'+str(numbers[i]) for i in range(len(numbers))]

numbers = [random.randint(1,100) for i in range(1000)]
profile = LineProfiler(do_stuff(numbers))
profile.print_stats()
[] Timer unit: 3.20721e-07 s
4

2 回答 2

45

line_profiler测试用例(在GitHub上找到)有一个如何从 Python 脚本中生成配置文件数据的示例。您必须包装要分析的函数,然后调用包装器传递任何所需的函数参数。

from line_profiler import LineProfiler
import random

def do_stuff(numbers):
    s = sum(numbers)
    l = [numbers[i]/43 for i in range(len(numbers))]
    m = ['hello'+str(numbers[i]) for i in range(len(numbers))]

numbers = [random.randint(1,100) for i in range(1000)]
lp = LineProfiler()
lp_wrapper = lp(do_stuff)
lp_wrapper(numbers)
lp.print_stats()

输出:

Timer unit: 1e-06 s

Total time: 0.000649 s
File: <ipython-input-2-2e060b054fea>
Function: do_stuff at line 4

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     4                                           def do_stuff(numbers):
     5         1           10     10.0      1.5      s = sum(numbers)
     6         1          186    186.0     28.7      l = [numbers[i]/43 for i in range(len(numbers))]
     7         1          453    453.0     69.8      m = ['hello'+str(numbers[i]) for i in range(len(numbers))]

向配置文件添加附加功能

此外,您还可以添加要分析的其他功能。例如,如果您有第二个被调用函数并且只包装调用函数,您将只能看到调用函数的配置文件结果。

from line_profiler import LineProfiler
import random

def do_other_stuff(numbers):
    s = sum(numbers)

def do_stuff(numbers):
    do_other_stuff(numbers)
    l = [numbers[i]/43 for i in range(len(numbers))]
    m = ['hello'+str(numbers[i]) for i in range(len(numbers))]

numbers = [random.randint(1,100) for i in range(1000)]
lp = LineProfiler()
lp_wrapper = lp(do_stuff)
lp_wrapper(numbers)
lp.print_stats()

以上只会为调用函数产生以下配置文件输出:

Timer unit: 1e-06 s

Total time: 0.000773 s
File: <ipython-input-3-ec0394d0a501>
Function: do_stuff at line 7

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     7                                           def do_stuff(numbers):
     8         1           11     11.0      1.4      do_other_stuff(numbers)
     9         1          236    236.0     30.5      l = [numbers[i]/43 for i in range(len(numbers))]
    10         1          526    526.0     68.0      m = ['hello'+str(numbers[i]) for i in range(len(numbers))]

在这种情况下,您可以像这样将额外的调用函数添加到配置文件中:

from line_profiler import LineProfiler
import random

def do_other_stuff(numbers):
    s = sum(numbers)

def do_stuff(numbers):
    do_other_stuff(numbers)
    l = [numbers[i]/43 for i in range(len(numbers))]
    m = ['hello'+str(numbers[i]) for i in range(len(numbers))]

numbers = [random.randint(1,100) for i in range(1000)]
lp = LineProfiler()
lp.add_function(do_other_stuff)   # add additional function to profile
lp_wrapper = lp(do_stuff)
lp_wrapper(numbers)
lp.print_stats()

输出:

Timer unit: 1e-06 s

Total time: 9e-06 s
File: <ipython-input-4-dae73707787c>
Function: do_other_stuff at line 4

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     4                                           def do_other_stuff(numbers):
     5         1            9      9.0    100.0      s = sum(numbers)

Total time: 0.000694 s
File: <ipython-input-4-dae73707787c>
Function: do_stuff at line 7

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     7                                           def do_stuff(numbers):
     8         1           12     12.0      1.7      do_other_stuff(numbers)
     9         1          208    208.0     30.0      l = [numbers[i]/43 for i in range(len(numbers))]
    10         1          474    474.0     68.3      m = ['hello'+str(numbers[i]) for i in range(len(numbers))]

注意:以这种方式将功能添加到配置文件不需要更改配置文件的代码(即,不需要添加@profile装饰器)。

于 2017-04-12T17:55:46.663 回答
9

文档中所述:

在您的脚本中,您可以使用@profile 装饰您想要分析的任何功能

你想装饰你的do_stuff功能,@profile然后运行

kernprof -v -l script_to_profile.py

将带注释的输出打印到您的终端。配置文件也将被写入script_to_profile.py.lprof,您可以稍后重新创建输出

python -m line_profiler script_to_profile.py.lprof
于 2014-07-11T17:41:07.230 回答