15

Possible Duplicate:
Commenting interpreted code and performance
Does comments affect when including files in PHP?

Let's say we have 100 class files and everytime when a page is requested, all these classes must be parsed by PHP.

Will PHP be slower if almost 1 half of the source code lines are the comments? Because usually I add a lot of comments & descriptions to code. This doesn't matter to compilers coz comments are not compiled, but PHP is interpreter, any bad thing may happen?

Community
  • 1
  • 1
jondinham
  • 8,271
  • 17
  • 80
  • 137

4 Answers4

15

Yes, but it's minimal, and this can (and should) be addressed entirely by using APC or another opcode cache. As a bonus, APC will speed everything else up as well.

If your site is slow, comments are not the reason.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
8

The only way it is slower, is that the interpreter has to read more bytes. But as for execution speed, it has no influence, because they are just ignored by the interpreter.

So basically, it does not matter if you add comments.

Ikke
  • 99,403
  • 23
  • 97
  • 120
  • 1
    but even if PHP wants to skip comment lines, it has to scan thru them to find the '/*' and the '*/' – jondinham Nov 23 '11 at 15:08
  • 5
    @Paul: PHP has to scan the entire file anyways. Consider how innefficient it is to have a 100k php file that contains exactly one tiny little `` in the middle of 99.99k of otherwise plain HTML. Unless you're running a facebook-sized operation, skipping comments is essentially "free" in PHP. – Marc B Nov 23 '11 at 15:10
  • 1
    @PaulDinh - Sure, but the overhead of that is tiny compared to the overhead of actually parsing and compiling code. – Polynomial Nov 23 '11 at 15:10
0

Sure, the parser has to do more work so it will be somewhat slower. In our project we used a PHP accelerator which did result in noticeable speed improvements. I'd recommend using an accelerator and then you can comment your code and not worry about performance.

bsegraves
  • 980
  • 13
  • 22
  • 2
    @MikeB Yes, it does. Cached opcode doesn't have to be reparsed each time, which means the comments don't get reparsed either. With opcode caching, you do not have to worry about the performance implications of lots of comments. – ceejayoz Nov 23 '11 at 15:08
  • it seems PHP is also compiled to bytecode? – jondinham Nov 23 '11 at 15:10
  • 2
    @Mike B: A PHP accelerator reduces the source code down to bytecodes. This process will remove any performance penalty (which is probably very minor) associated with comments. – bsegraves Nov 23 '11 at 15:11
  • @ceejayoz I stand corrected. Thanks for explaining. – Mike B Nov 23 '11 at 15:12
0

The situation you described will not make it any slower in any significant way. The parser will see /* or // and simply skip to the next */ or newline respectively. However, if you have 50k lines of comments or something silly like that, it may slow the parser a little whilst it skips over them.

If you're looking for a way to speed things up, APC and memcached are great solutions.

Polynomial
  • 27,674
  • 12
  • 80
  • 107