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
装饰器).
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_profiler
v1.0中,该kernprof
实用程序是作为可执行文件分发的,而不是.py
像我上面编写时那样的脚本文件.这意味着现在需要使用以下命令来从命令行调用它:
> "C:\Python27\Scripts\kernprof.exe" -l -v example.py
Run Code Online (Sandbox Code Playgroud)
小智 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
%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)
%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)
归档时间: |
|
查看次数: |
26607 次 |
最近记录: |