Comments affect Perl performance?

I optimize some commonly used Perl codes (once a day per file).

Are there any comments slow perl scripts? My experiments are useless:

use Benchmark; timethese(20000000, { 'comments' => '$b=1; # comment ... (100 times) ', 'nocomments' => '$b=1;'}); 

It gives almost the same values ​​(except for 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 comments and non-comment versions as separate Perl scripts.

It seems to contradict intuition, although nowhere does the interpreter have to read comments in memory every time.

+7
performance comments perl
source share
9 answers

Perl is a compiled language right at the point in time, so comments and PODs do not affect runtime performance.

Comments and PODs have little effect at compile time, but they are so light and fast for Perl to parse it is almost impossible to measure performance impact. You can see this for yourself using the -c flag to compile.

On my Macbook, a Perl program with 2 statements and 1000 lines of 70 characters comments at the same time to compile one of 1000 lines of blank comments as one with two print statements. Be sure to run each test twice to allow your OS to cache the file, otherwise you are comparing the read time of the file from disk.

If startup time is a problem for you, it is not related to comments and POD.

+13
source share

Runtime performance? Not.

Analysis and linguistic performance? Oh sure.

As Perl strives to parse and lex on-the-fly, comments will affect run-time performance.

Will they affect this noticeably? Unlikely.

+18
source share

Perl compiles the script and then executes it. Comments slightly slow down the compilation phase, but have zero effect at run time.

+8
source share

Perl is not a scripting language in the same sense as shell scripts. The interpreter does not read the file line by line. Perl program execution is carried out in two main stages: compilation and runtime [1]. At the compilation stage, the source code is analyzed and converted to bytecode. At the execution stage, the byte code is executed in a virtual machine.

Comments slow down the parsing phase, but the difference is not significant compared to the time needed to parse the script itself (which is already very small for most programs). The only time you really care about the parsing time is a web server environment where a program can be called many times per second. There is mod_perl to solve this problem.

You are using Benchmark . It's good! You should look for ways to improve the algorithm - not micro-optimization. Devel :: DProf can be useful to find any hot spots. You should absolutely not deny comments in an erroneous attempt to make your program faster. You just make it unattainable.


[1] This is commonly referred to as time-only compilation. Perl actually has a few more steps, such as INIT and END , which are irrelevant here.

+7
source share

Point: bottleneck optimization. Reading in a file consists of:

  • Opening a file
  • reading in its content,
  • file closing
  • parsing content.

Of these steps, reading is the fastest part to date (I'm not sure about closing, it's syscall, but you don't need to wait for it to complete). Even if this is 10% of everything that I think), then reducing it by half gives only 5% improved performance due to missing comments (which is very bad). For the analyzer, dropping a line starting with C # is not a tangible slowdown. And after that, the comments disappeared, so there can be no decline.

Now imagine that you could improve “reading in a script” by 5% by deleting all comments (which is a really optimistic estimate, see above). How large is the share of “reading in a script” in the total script time consumption? Of course, it depends on how important this is, but since perl scripts usually read at least one more file, this is not more than 50%, but since perl scripts usually do something else, an honest assessment will lead to something in the range of 1% . Thus, the expected increase in efficiency by removing all comments is no more (very optimistic) by 2.5%, but really closer to 0.05%. And then those where it actually gives more than 1% are already fast, since they do almost nothing, so you are optimizing again at the wrong point.

Completion, bottleneck optimization.

+3
source share

In this case, the Benchmark module is useless. This is just a measurement of the time to run the code again and again. Since your code does virtually nothing, most of it is optimized. That is why you see that it works 22 million times per second.

I have almost an entire chapter about this in Mastering Perl . The measurement error in the Benchmark method is about 7%. Your control numbers are good at this, so there is virtually no difference.

+2
source share

From a comment by Paul Tomblins:

Does perl do some compilation on the fly? Maybe comments will be discarded earlier? -

Yes, Perl.

It is a programming language between compiled and interpreted. The code compiles on the fly and then runs. comments usually have no meaning. Most likely, this will happen when he first analyzes the file line by line and pre-compiles it, you can see the difference in nano-second.

+1
source share

I would expect that one comment will be processed once, and not several times in a loop, so I doubt that this is a valid test.

I would expect the comments to be a bit slow compilation, but I expect it to be too small to bother deleting.

0
source share

Do Perl comments slower script down? Well, disassembling it, yes. Doing this after parsing? Not. How often is the script parsed? Only once, therefore, if you have a comment in a for loop, the comment is parsed once before the script even runs as soon as it starts, the comment has already disappeared (and the script is not saved as a script inside Perl), so regardless how many times the for loop repeats, the comment will have no effect. How fast can the parser skip comments? The way Perl comments are written is very fast, so I doubt you will notice. You will notice a higher launch time if you have 5 lines of code and between each line of 1 Mio lines of comments ... but how much is possible and what comment will be used, which will be large?

0
source share

All Articles