Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

by liz (Monsignor)
on Aug 14, 2006 at 21:45 UTC ( #567347=note: print w/ replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://567347]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (12)
As of 2014-09-01 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (9 votes), past polls