我正在优化一些经常运行的Perl代码(每个文件每天一次).
评论是否会降低Perl脚本的速度?我的实验倾向于否:
- use Benchmark;
- timethese(20000000,{
- 'comments' => '$b=1;
- # comment ... (100 times)
- ','nocomments' => '$b=1;'});
提供几乎相同的值(除了噪音).
- 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)
如果我将注释和无注释版本作为单独的Perl脚本运行,我会得到类似的结果.
这似乎是违反直觉的,如果没有别的,解释器每次都需要将注释读入内存.