如何分析我的 Perl 程序?
I need to improve the performance of my Perl application. How can I find the slow spots?
This is a question from the official perlfaq. We're importing the perlfaq to Stack Overflow.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(这是 官方 perlfaq 答案,减去任何后续编辑)
Devel
命名空间有您可以使用的几个模块分析您的 Perl 程序。 Devel::DProf 模块随 Perl 一起提供,您可以使用
-d
switch:在
DProf
下运行程序后,您将获得一个 tmon.out 文件与配置文件数据。要查看数据,您可以将其转换为
使用附带的
dprofpp
程序生成人类可读的报告Devel::DProf
:您还可以使用
-p
一步完成分析和报告切换到
dprofpp
:Devel::NYTProf(纽约时报分析器)同时执行这两项操作语句和子程序分析。它可以从 CPAN 获得,您还可以调用
它与
-d
开关一起使用:与
DProf
一样,它会创建一个包含您的个人资料信息的数据库。可以变成报告。
nytprofhtml
命令将数据转换为类似于 Devel::Cover 报告的 HTML 报告:
CPAN 还有多个其他分析器,您可以在同一报告中调用
时尚。您可能还对使用 C 感兴趣
测量并比较代码片段。
您可以在Perl 编程第 20 章中阅读有关分析的更多信息,
或掌握 Perl,第 5 章。
perldebguts 文档创建自定义调试器(如果您需要)
创建一种特殊类型的分析器。 Brian d Foy 描述了这个过程
The Perl Journal、“创建 Perl 调试器” 和 “Perl 中的分析”。
Perl.com 有两篇关于分析的有趣文章:“Profiling Perl”,
作者:Simon Cozens,以及“调试和
分析 mod_perl 应用程序”,作者:Frank Wiles。Randal
L. Schwartz 在“加速 Perl”中撰写了有关分析的文章
程序” for Unix Review 和 “分析
Linux 杂志的 Template Toolkit via Overriding”。
(This is the official perlfaq answer, minus any subsequent edits)
The
Devel
namespace has several modules which you can use toprofile your Perl programs. The Devel::DProf module comes with Perl and you can invoke it with the
-d
switch:After running your program under
DProf
, you'll get a tmon.out filewith the profile data. To look at the data, you can turn it into a
human-readable report with the
dprofpp
program that comes withDevel::DProf
:You can also do the profiling and reporting in one step with the
-p
switch to
dprofpp
:The Devel::NYTProf (New York Times Profiler) does both statement and subroutine profiling. It's available from CPAN and you also invoke
it with the
-d
switch:Like
DProf
, it creates a database of the profile information that youcan turn into reports. The
nytprofhtml
command turns the data intoan HTML report similar to the Devel::Cover report:
CPAN has several other profilers that you can invoke in the same
fashion. You might also be interested in using the C to
measure and compare code snippets.
You can read more about profiling in Programming Perl, chapter 20,
or Mastering Perl, chapter 5.
perldebguts documents creating a custom debugger if you need to
create a special sort of profiler. brian d foy describes the process
in The Perl Journal, "Creating a Perl Debugger", and "Profiling in Perl".
Perl.com has two interesting articles on profiling: "Profiling Perl",
by Simon Cozens, and "Debugging and
Profiling mod_perl Applications", by Frank Wiles.
Randal L. Schwartz writes about profiling in "Speeding up Your Perl
Programs" for Unix Review and "Profiling
in Template Toolkit via Overriding" for Linux Magazine.
我已改用 Devel::NYTProf,这是最好的分析Perl 的组合,最初是由《纽约时报》的人提出的。
I've switched over to using Devel::NYTProf, which is all the best profiling for Perl combined, initially by the folks at the NYTimes.
有一种非常简单的方法可以找到慢点,以便提高程序的性能 - 随机暂停。
基本上,这个想法是,你不是通过测量来看看哪个部分是慢的,而是让它的慢度暴露给你。
使用调试标志
-d
运行程序,并在运行时手动中断它,并显示调用堆栈 (T
)。执行此操作几次,例如 5 或 10 次。查找出现在多个堆栈上的任何语句,和这不是绝对必要的,因为它所花费的时间大致是堆栈的百分比表明它。这不仅可以找到热点,还可以找到调用成本高昂的函数的线路。无论程序是 I/O 还是 CPU 密集型,它都可以正常工作,并且机器中发生的其他事情并不重要。
你可以多次这样做,直到你再也找不到任何可以加速的东西。
There's a very simple way to find the slow spots so you can improve the performance of your program - random-pausing.
Basically, the idea is, rather than measure to see what part is slow, you let its slowness expose it to you.
Run the program with the debug flag
-d
, and while it's running, interrupt it manually, and display the call stack (T
). Do this a few times, like 5 or 10. Look for any statement that appears on more than one stack and that isn't strictly necessary, because the time it is responsible for is roughly the percent of stacks that show it.This finds not only hotspots, but lines where functions are being called expensively. It works just as well whether the program is I/O or CPU bound, and it doesn't matter what else is going on in the machine.
You can do it more than once, until you can no longer find anything that can be speeded up.