如何在jupyter notebook 5中逐行描述python 3.5代码

时间:2017-06-24 08:01:52

标签: python python-3.x profiling jupyter-notebook

如何找出每行python代码所花费的执行时间。

line_profiler与ipython一起使用,但不能与jupyter笔记本一起使用。我尝试将@profile添加到我的函数中,它给出了错误名称' profile'没有定义。 有一种方法可以通过time.time()来完成它,但我想知道是否有任何内置的分析功能可以分析我的功能的每一行并显示执行时间。

def prof_function():
    x=10*20
    y=10+x
    return (y)

4 个答案:

答案 0 :(得分:5)

要获得每行的执行时间并获得漂亮的颜色编码热图,我使用了这个漂亮的ipython魔术。...https://github.com/csurfer/pyheatmagic

enter image description here

安装:

  

pip安装py-heat-magic

要配置笔记本中的每一行:

  • 复制笔记本。
  • 合并所有单元格(突出显示所有和shift-m)
  • 在顶部创建一个新单元格
  • 输入

%load_ext heat

在第二个单元格的顶部的第一行中输入以下内容:

%%heat  

如果您有超过2000行代码,则可能会遇到问题。

答案 1 :(得分:3)

您可以在jupyter笔记本中使用line_profiler

  1. 安装它:pip install line_profiler
  2. 在您的jupyter笔记本中,请致电:%load_ext line_profiler
  3. 在示例中定义您的函数prof_function
  4. 最后,简介如下:%lprun -f prof_function prof_function()
  5. 将提供输出:

    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