我如何使用line_profiler(来自Robert Kern)?

Ale*_*kov 18 python profiling python-2.7 line-profiler

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

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

2)写了一小段代码(类似于此处另一个回答问题中提到的代码).

from line_profiler import LineProfiler
def do_stuff(numbers):
    print numbers

numbers = 2
profile = LineProfiler(do_stuff(numbers))
profile.print_stats()
Run Code Online (Sandbox Code Playgroud)

3)运行IDLE/PyScripter中的代码.我只得到了时间.

Timer unit: 4.17188e-10 s
Run Code Online (Sandbox Code Playgroud)

如何在我执行的代码上获得完整的逐行配置文件?我从未使用任何高级Python功能,如装饰器,所以我很难理解如何使用这里这里的几个帖子提供的指南.

tdu*_*ube 35

这个答案是我在这里的答案的副本,关于如何line_profiler从Python脚本中获取统计数据(不使用kernprof命令行或不必将@profile装饰器添加到函数和类方法).对类似line_profiler问题的所有答案(我已经看到)只描述了使用kernprof.


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()
Run Code Online (Sandbox Code Playgroud)

输出:

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))]
Run Code Online (Sandbox Code Playgroud)

向配置文件添加附加功能

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

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()
Run Code Online (Sandbox Code Playgroud)

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

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))]
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您可以将其他被调用函数添加到配置文件中,如下所示:

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()
Run Code Online (Sandbox Code Playgroud)

输出:

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))]
Run Code Online (Sandbox Code Playgroud)

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

  • 好答案!但是我实际上发现装饰器是控制性能分析的好方法。有没有一种方法可以将line_prof与脚本内的装饰器一起使用(不使用kernprof)? (2认同)

mar*_*eau 13

只需按照第一个链接中的 Dan Riti示例,即可使用您的代码.安装line_profiler模块后你要做的就是@profile在每个你希望逐行分析的函数之前添加一个装饰器,并确保每个函数至少在代码中的其他地方调用一次 - 所以对于你的简单示例代码来说是这样的:

example.py 文件:

@profile
def do_stuff(numbers):
    print numbers

numbers = 2
do_stuff(numbers)
Run Code Online (Sandbox Code Playgroud)

已经这样做了,通过运行脚本kernprof.py已安装在你的C:\Python27\Scripts目录中.这是在Windows 7命令行会话中执行此操作的(不是非常有趣)实际输出:

> 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
Run Code Online (Sandbox Code Playgroud)

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

更新

看来在line_profilerv1.0中,该kernprof实用程序是作为可执行文件分发的,而不是.py像我上面编写时那样的脚本文件.这意味着现在需要使用以下命令来从命令行调用它:

> "C:\Python27\Scripts\kernprof.exe" -l -v example.py
Run Code Online (Sandbox Code Playgroud)

  • @ Alex`kernprof`不必具有`.py`扩展名即可执行.我安装在一个venv内,并且运行良好就像`kernprof`. (4认同)
  • 别客气.顺便说一句,如果你想正常运行脚本(没有`kernprof.py`),你需要删除`@ profile`装饰器调用或定义你自己的虚拟调用:例如`profile = lambda f:f`在文件的开头. (3认同)

小智 8

line_profiler发现使用装饰器的一个很好的用途,即@profile对我有用:

def profile(func):
    from functools import wraps

    @wraps(func)
    def wrapper(*args, **kwargs):
        from line_profiler import LineProfiler
        prof = LineProfiler()
        try:
            return prof(func)(*args, **kwargs)
        finally:
            prof.print_stats()

    return wrapper
Run Code Online (Sandbox Code Playgroud)

致谢:pavelpatrin


Mah*_*sam 6

加载 line_profiler 和 numpy

%load_ext line_profiler
import numpy as np
Run Code Online (Sandbox Code Playgroud)

定义一个函数例如:

def take_sqr(array):
    sqr_ar = [np.sqrt(x) for x in array]
    return sqr_ar
Run Code Online (Sandbox Code Playgroud)

使用line_profiler来统计时间如下:

%lprun -f take_sqr take_sqr([1,2,3])
Run Code Online (Sandbox Code Playgroud)

输出如下所示:

Timer unit: 1e-06 s
 
Total time: 6e-05 s File: <ipython-input-5-e50c1b05a473> Function:
take_sqr at line 1

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     1                                           def take_sqr(array):
     2         4         59.0     14.8     98.3      sqr_ar = [np.sqrt(x) for x in array]
     3         1          1.0      1.0      1.7      return sqr_ar
Run Code Online (Sandbox Code Playgroud)

  • 欢迎来到堆栈溢出!仅代码答案并不是特别有帮助。请简要描述此代码如何解决问题。 (3认同)