关于python:如何使用line_profiler(来自Robert Kern)?

How do I use line_profiler (from Robert Kern)?

我尝试使用line_profiler模块通过Python文件获取逐行配置文件。到目前为止,这是我所做的:

1)使用.exe文件从pypi安装了line_profiler(我在WinXP和Win7上)。只需单击安装向导即可。

2)写了一小段代码(类似于此处另一个已回答问题中的要求)。

1
2
3
4
5
6
7
from line_profiler import LineProfiler
def do_stuff(numbers):
    print numbers

numbers = 2
profile = LineProfiler(do_stuff(numbers))
profile.print_stats()

3)从IDLE / PyScripter运行代码。我只有时间。

1
Timer unit: 4.17188e-10 s

如何对执行的代码获得完整的逐行配置文件?我从未使用过装饰器之类的任何高级Python功能,因此很难理解如何使用此处和此处的几篇文章提供的指南。


此答案是我的答案的副本,内容是如何从Python脚本中获取line_profiler统计信息的(无需从命令行使用kernprof或不必向函数和类方法添加@profile装饰器)。对类似line_profiler问题的所有答案(我见过的)仅使用kernprof描述。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
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()

输出:

1
2
3
4
5
6
7
8
9
10
11
12
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))]

向配置文件添加其他功能

此外,您还可以添加其他要分析的功能。例如,如果您有第二个被调用函数,并且仅包装了调用函数,那么您只会看到来自调用函数的概要文件结果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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()

上面只会为调用函数产生以下概要文件输出:

1
2
3
4
5
6
7
8
9
10
11
12
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))]

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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()

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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装饰器)。


只需遵循第一个链接中Dan Riti的示例,但使用您的代码。在安装line_profiler模块之后,您要做的就是在要逐行分析的每个函数之前添加一个@profile装饰器,并确保在代码中的其他位置至少每个函数被调用一次。对于您这样的琐碎示例代码:

example.py文件:

1
2
3
4
5
6
@profile
def do_stuff(numbers):
    print numbers

numbers = 2
do_stuff(numbers)

已完成此操作,请通过kernprof.py a运行脚本?安装在您的C:\\Python27\\Scripts目录中。这是在Windows 7命令行会话中执行此操作的(不太有趣的)实际输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
> python"C:\\Python27\\Scripts\\kernprof.py" -l -v example.py
2
Wrote profile results to example.py.lprof
Timer unit: 3.2079e-07 s

File: example.py
Function: do_stuff at line 2
Total time: 0.00185256 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     1                                           @profile
     2                                           def do_stuff(numbers):
     3         1         5775   5775.0    100.0      print numbers

您可能需要调整最后一步-"使用kernprof.py而不是直接通过Python解释器运行测试脚本",以便在IDLE或PyScripter中进行等效操作。

a ??更新

似乎在line_profiler v1.0中,kernprof实用程序以可执行文件的形式分发,而不是像我编写上面的脚本时的.py脚本文件。这意味着现在需要使用以下命令从命令行调用它:

1
>"C:\\Python27\\Scripts\\kernprof.exe" -l -v example.py