事件探查器

这个 事件探查器 pane递归地确定文件中调用的每个函数和方法的运行时间和调用次数,将每个过程分解为其最小的独立单元。这使您可以轻松地识别代码中的瓶颈,指出对优化最关键的语句,并在后续更改之后测量性能增量。

Spyder Profiler pane, displaying a list of functions and their execution time

运行探查器

可以使用右侧的“打开”按钮浏览文件 Profiler 的路径框(窗格左上角),它将自动对此文件运行分析。

Spyder Profiler pane, showing browsing a file and running profiler

您可以在窗格的“路径”框中手动输入路径,然后按对文件运行分析 Profile 在探查器窗格中。

还可以对当前在中打开的文件运行分析 编辑 点击 Run ‣ Profile 在菜单栏中,或使用可配置的快捷方式 (F10 默认情况下)。

Spyder Profiler pane, showing running profiler from menu bar

如果要取消正在进行的运行,请单击 Stop 按钮,如果由于任何原因分析失败 Output 对话框将显示,指示发生的错误。

通过双击探查器中的某个项,您将被带到该文件和 编辑 它的名字。

Spyder Profiler pane, showing opening a file when clicking on its analysis

通过单击名称左侧的下拉箭头,可以增加为特定对象显示的级别数,并使用左上角的按钮展开/折叠所有项目。

Spyder Profiler pane, showing dropdown arrows and buttons for expanding and collapsing

单击下拉列表或按 Down Arrow 在“文件名”字段中,可以调用以前分析的文件的路径。

Spyder Profiler pane, showing dropdown of previous profiled files

最后,您可以使用 .Result 使用 Save data 按钮。可以加载此文件以与使用 Load data 按钮。要删除加载的数据,请单击 Clear comparison 按钮。

Spyder Profiler pane, showing running profiler from menu bar

解释结果

结果按函数/方法/语句细分,每个子元素按层次结构列在调用它们的顶层项下。 Total Time 是由指定的项和“下面”的每个函数获取的吗( i.e. 呼叫)它,而 Local Time 只计算在特定可调用对象自身作用域中花费的时间。这个 Calls 列显示指定对象在其父调用函数(或 __main__ 范围,如果是顶级对象)。最后,在 Diff 如果加载了比较,将显示三个测量值中每一个的列,并指示每个测量值之间的增量。

Profiler with a comparison loaded, displaying the time deltas between two runs

例如,假设您运行 Profiler 在调用函数的文件上 sleep_wrapper() 它又称之为 sleep() 函数,以及 sleep_wrapper() 函数运行总共花费了3.66毫秒,其中3.64毫秒用于执行 sleep() 内部功能。因此,如果 sleep() 它本身不叫别的东西 Total TimeLocal Time 两者都是一样的,在3点64分,同时, Total Time 对于 sleep_wrapper() 会是3.66毫秒,但是 Local Time 只有0.02毫秒,其余时间在 sleep() 它调用的函数。

探查器插件

您可以安装两个附加插件来启用Spyder中的其他类型的评测。首先,Spyder Line Profiler允许您分别对代码的每一行进行基准测试。要了解更多信息,请访问 spyder-line-profiler git repository .

Spyder Profiler pane, displaying a list of functions and their execution time

其次,Spyder Memory Profiler测量代码的内存使用情况。更多信息,请访问 spyder-memory-profiler git repository .

Spyder Profiler pane, displaying a list of functions and their execution time