是否影响Perl性能? [英] Do comments affect Perl performance?

查看:127
本文介绍了是否影响Perl性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我优化了一些经常运行的Perl代码(每个文件每天一次)。

I'm optimizing some frequently run Perl code (once per day per file).

做评论慢perl脚本下来?我的实验倾向于:

Do comments slow Perl scripts down? My experiments lean towards no:

use Benchmark;
timethese(20000000, {
    'comments' => '$b=1;
# comment  ... (100 times)
', 'nocomments' => '$b=1;'});

提供几乎相同的值(除了噪音)。

Gives pretty much identical values (apart from noise).

Benchmark: timing 10000000 iterations of comments, nocomments...
  comments:  1 wallclock secs ( 0.53 usr +  0.00 sys =  0.53 CPU) @ 18832391.71/s (n=10000000)
nocomments:  0 wallclock secs ( 0.44 usr +  0.00 sys =  0.44 CPU) @ 22935779.82/s (n=10000000)

Benchmark: timing 20000000 iterations of comments, nocomments...
  comments:  0 wallclock secs ( 0.86 usr + -0.01 sys =  0.84 CPU) @ 23696682.46/s (n=20000000)
nocomments:  1 wallclock secs ( 0.90 usr +  0.00 sys =  0.90 CPU) @ 22099447.51/s (n=20000000)



如果我运行注释和no-评论版本作为单独的Perl脚本。

I get similar results if I run the comments and no-comments versions as separate Perl scripts.

这似乎是反直觉的,如果没有别的解释器需要读取的内容每次的意见。

It seems counter-intuitive though, if nothing else the interpreter needs to read the comments into memory every time.

推荐答案

Perl是一种即时编译语言,因此注释和POD对运行时性能没有影响。

Perl is a just-in-time compiled language, so comments and POD have no effect on run-time performance.

注释和POD对编译时间有微小的影响,但是它们非常容易和快速地解析Perl几乎不可能测量性能损失。您可以使用 -c 标志来编译。

Comments and POD have a minuscule effect on compile-time, but they're so easy and fast for Perl to parse it's almost impossible to measure the performance hit. You can see this for yourself by using the -c flag to just compile.

在我的Macbook上,程序具有2个语句和1000行的70个字符注释,需要相同的时间来编译为具有1000行空注释的程序,只有2个打印语句。确保每次运行两次基准测试两次,以允许操作系统缓存文件,否则您的基准测试是从磁盘读取文件的时间。

On my Macbook, a Perl program with 2 statements and 1000 lines of 70 character comments takes the same time to compile as one with 1000 lines of empty comments as one with just 2 print statements. Be sure to run each benchmark twice to allow your OS to cache the file, otherwise what you're benchmarking is the time to read the file from the disk.

如果启动时间是一个问题,这不是因为评论和POD。

If startup time is a problem for you, it's not because of comments and POD.

这篇关于是否影响Perl性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆