Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

by BrowserUk (Pope)
on Aug 12, 2006 at 03:54 UTC ( #567003=note: print w/ replies, xml ) Need Help??


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

With 100 lines of comments to 2 lines of code, versus 100 blank lines to 2 lines of code, the commented code takes 4.3 e-5 seconds longer to load & compile. Do away with the blank lines and it takes 10e-5 seconds longer.

So, even at this ludicrously high comment to code ratio, the time taken to process the comments is maybe 6/100,000 ths of a second.

And that happens just once per run of the code.

The differences shown in the runtime of the code are so small that they are influenced by whether you move the mouse during the test or not--and in my tests usually favoured the commented code! Atmospheric temperature changing the resistance of the cpu probably has more influence on the performance than the presence of absence of comments in the code. This is where benchmarking becomes totally inadequate.

Even the 16% difference below represents a real-time difference of 1.0e-7 of a second. That's one whole tenth of a microsecond.

#! perl -slw use strict; use Benchmark qw[ cmpthese ]; open O, '>', 'with.pl'; print O '# a comment', ' ' x 60, for 1 .. 100; print O 'sub with{ $_[ 0 ]++; }'; print O 'our $with++'; close O; open O, '>', 'without.pl'; print O ' ' x 80, for 1 .. 100; print O 'sub without{ $_[ 0 ]++; }'; print O 'our $without++'; close O; our( $with, $without ); { local $^W; ( $with, $without ) = ( 0 ) x 2; cmpthese -1, { compile_with => q[ do 'with.pl'; ], compile_without => q[ do 'without.pl'; ] }; print "With:$with without:$without\n"; } { ( $with, $without ) = ( 0 ) x 2; cmpthese -1, { A_call_with => q[ with( $with ); ], B_call_without => q[ without( $without ); ] }; print "With:$with without:$without\n"; } { ( $with, $without ) = ( 0 ) x 2; cmpthese -1, { B_call_with => q[ with( $with ); ], A_call_without => q[ without( $without ); ] }; print "With:$with without:$without\n"; } __END__ C:\test>junk Rate compile_without compile_with compile_without 3046/s -- -13% compile_with 3507/s 15% -- With:4863 without:4447 Rate B_call_without A_call_with B_call_without 1541400/s -- -4% A_call_with 1602739/s 4% -- With:2476423 without:2258362 Rate A_call_without B_call_with A_call_without 1552179/s -- -5% B_call_with 1632288/s 5% -- With:2386728 without:2124584 C:\test>junk Rate compile_without compile_with compile_without 3001/s -- -11% compile_with 3368/s 12% -- With:4863 without:4447 Rate B_call_without A_call_with B_call_without 1405856/s -- -14% A_call_with 1630859/s 16% -- With:2386728 without:1996218 Rate A_call_without B_call_with A_call_without 1480385/s -- -11% B_call_with 1655503/s 12% -- With:2386728 without:2142348 C:\test>junk Rate compile_without compile_with compile_without 3023/s -- -10% compile_with 3365/s 11% -- With:4863 without:4469 Rate B_call_without A_call_with B_call_without 1671100/s -- -0% A_call_with 1676092/s 0% -- With:2277479 without:2250533 Rate A_call_without B_call_with A_call_without 1575095/s -- -4% B_call_with 1636763/s 4% -- With:2288717 without:2124584

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re: Are there any drawbacks to comments -- can they hurt the performance of Perl code?
Download Code
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
    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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2014-12-22 11:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (116 votes), past polls