如何找出每行python代码所花费的执行时间。
line_profiler与ipython一起使用,但不能与jupyter笔记本一起使用。我尝试将@profile添加到我的函数中,它给出了错误名称' profile'没有定义。 有一种方法可以通过time.time()来完成它,但我想知道是否有任何内置的分析功能可以分析我的功能的每一行并显示执行时间。
def prof_function():
x=10*20
y=10+x
return (y)
答案 0 :(得分:5)
要获得每行的执行时间并获得漂亮的颜色编码热图,我使用了这个漂亮的ipython魔术。...https://github.com/csurfer/pyheatmagic
安装:
pip安装py-heat-magic
要配置笔记本中的每一行:
%load_ext heat
在第二个单元格的顶部的第一行中输入以下内容:
%%heat
如果您有超过2000行代码,则可能会遇到问题。
答案 1 :(得分:3)
您可以在jupyter笔记本中使用line_profiler
。
pip install line_profiler
%load_ext line_profiler
prof_function
。 %lprun -f prof_function prof_function()
将提供输出:
Timer unit: 1e-06 s
Total time: 3e-06 s
File: <ipython-input-22-41854af628da>
Function: prof_function at line 1
Line # Hits Time Per Hit % Time Line Contents
==============================================================
1 def prof_function():
2 1 1.0 1.0 33.3 x=10*20
3 1 1.0 1.0 33.3 y=10+x
4 1 1.0 1.0 33.3 return (y)
答案 2 :(得分:2)
安装line profiler
conda install line_profiler
有关http://mortada.net/easily-profile-python-code-in-jupyter.html
的更多信息答案 3 :(得分:2)
只是@ S.A。答案的摘要
SELECT
CAST(PolicyNumber AS varchar(50)) POLICYNUMBER,
CAST(EFFECTIVEDATE AS DATE) EFFECTIVEDATE ,
MAX(CAST(EXPIRATIONDATE AS DATE)) EXPIRATIONDATE,
CAST(ISSUINGCOMPANY AS varchar(4)) AS INSURER,
CAST(INSUREDLEGALNAME AS varchar(250)) AS INSUREDNAME,
PROGRAM,
MAX(CAST(CANCELDATE AS DATE)) AS CANCELDATE,
CAST(INSUREDADDR1 AS varchar(500)) AS INSUREDADDR1,
CAST(INSUREDADDR2 AS varchar(500)) AS INSUREDADDR2,
CAST(INSUREDCITY AS varchar(500)) AS INSUREDCITY,
CAST(INSUREDSTATE AS varchar(20)) AS INSUREDSTATE,
CAST(INSUREDZIP AS varchar(20)) AS INSUREDZIP ,
(SELECT TOP 1 SourceId FROM STG.[Source] WHERE [SystemName] = 'CRU') AS SOURCESYSTEMID
,(Select TOP 1 ProgramInclude = CASE WHEN ProgramInClude = 'Y' THEN 1 Else 0 END
From STG.LPT L WHERE L.ProgramCode = op.Program ) AS LPT
FROM SRC.Policy AS op
GROUP BY POLICYNUMBER,
EFFECTIVEDATE,
ISSUINGCOMPANY,
INSUREDLEGALNAME,
PROGRAM,
INSUREDADDR1,
INSUREDADDR2,
INSUREDCITY,
INSUREDSTATE,
INSUREDZIP