Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do comments affect Perl performance?

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

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)

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.

like image 627
Dave Avatar asked Nov 06 '08 19:11

Dave


4 Answers

Runtime performance? No.

Parsing and lexing performance? Yes, of course.

Since Perl tends to parse and lex on the fly, then comments will affect "start up" performance.

Will they affect it noticably? Unlikely.

like image 125
Will Hartung Avatar answered Sep 22 '22 06:09

Will Hartung


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

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.

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.

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

like image 31
Schwern Avatar answered Sep 24 '22 06:09

Schwern


Perl compiles a script and then executes it. Comments marginally slow the compile phase, but have zero effect on the run phase.

like image 23
Jonathan Leffler Avatar answered Sep 22 '22 06:09

Jonathan Leffler


Perl is not a scripting language in the same sense that shell scripts are. The interpreter does not read the file line by line. The execution of a Perl program is done in two basic stages: compilation and runtime [1]. During the compilation stage the source code is parsed and converted into bytecode. During the runtime stage the bytecode is executed on a virtual machine.

Comments will slow down the parsing stage but the difference is negligible compared to the time required to parse the script itself (which is already very small for most programs). About the only time you're really concerned with parsing time is in a webserver environment where the program could be called many times per second. mod_perl exists to solve this problem.

You're using Benchmark. That's good! You should be looking for ways to improve the algorithm -- not micro-optimizing. Devel::DProf might be helpful to find any hot spots. You absolutely should not strip comments in a misguided attempt to make your program faster. You'll just make it unmaintainable.


[1] This is commonly called "just in time" compilation. Perl actually has several more stages like INIT and END that don't matter here.

like image 40
Michael Carman Avatar answered Sep 23 '22 06:09

Michael Carman