http://www.perlmonks.org?node_id=567347


in reply to Re: Are there any drawbacks to comments -- can they hurt the performance of Perl code?
in thread Are there any drawbacks to comments -- can they hurt the performance of Perl code?

Unfortunately, I think there is at least *one* corner case where having comments actually do make a difference: extended regular expressions with interpolated variables with or without comments.

Mind you, you're going to have to do a lot of regular expressions before you actually start noticing this.

use Benchmark qw(:hireswallclock cmpthese); my $bappo = 'bappo'; my $foo = "foo bar baz bippo $bappo zappo"; cmpthese( -3, { extended => sub { $foo =~ / $bappo \s+ (zappo) /x; }, comment => sub { $foo =~ / $bappo # this is a comment \s+ # inside an extended (zappo) # regular expression /x; }, no_comment => sub { $foo =~ /$bappo\s+(zappo)/ }, } ); __END__ Rate comment extended no_comment comment 480920/s -- -11% -22% extended 542294/s 13% -- -12% no_comment 614847/s 28% 13% --

The reason for this is most likely because of the variable interpolation (so Perl cannot store a compiled version of the regular expression) and the fact that extended comments are actually part of the string when the regular expression is being compiled.

Of course, most regular expressions that warrant the extended format with comments, are also the more complicated ones, so in the real world, this might still be a bit worse. (I leave this as an excercise for the reader).

On the other hand, saving one hour of your coworkers time when (s)he has to look at your code, is usually worth much more, so your mileage may vary ;-)

Liz

  • Comment on Re^2: Are there any drawbacks to comments -- can they hurt the performance of Perl code?
  • Download Code