Documenting PHP code - does it hurt performance?

I can't believe anyone usually objects to documentation and comments, but what is the best practice (and practical) for doing this in PHP?

In JavaScript, you can document your code and then run a minimizer to create a production version of your code for use. What in PHP, are additional lines of text added for performance? If you keep your documents in another file (which, I suppose, may be important for APIs and frameworks, but it will slow down your development)?

Edit:

My logic was that it would certainly take longer because the file size is larger and will add more work (although this is possible and slightly) for the analyzer. Let's say you had 1000 lines of code and 500 lines of comments, should you add a generic version instead, and then go to the actual documentation page?

+4
source share
10 answers

With PHP (for example, ASP.NET), the code can be compiled on the server, and the HTML generated to be sent over the pipe to the client.

PHP source code is compiled on the fly into an internal format that can be run by the PHP engine. To speed up execution time and not need to compile PHP source code every time you access a web page, PHP scripts can also be deployed in executable format using the PHP compiler.

A source

During this process, comments are ignored and not involved in the proceedings. They do not slow down the compiler and therefore do not affect performance.

You should comment on your code (where necessary) and leave comments with the source code. Otherwise, there is an even greater chance that they will be out of order with the code, and then worse than useless.

+7
source

If you assume the cost of the processor is O (1) for each comment character, and your code is 4k, and half of the code is comments, you lose the basic 2k operations. With modern processors, the basic operation is ~ 20 clock cycles, but since there is pipeline optimization, we can assume perhaps 5 cycles per operation. Since we are dealing with gigahertz processors these days, 2k / 1g * 5 ~ = 1 / 10000th of a second is wasted on parsing comments.

Comment your code!

Minor items:

  • 4k is one block of partitions, so disk reading is minimized. You still have to go to the program code disk. The processors have enough cache, so the whole program will be stored in memory, comments, etc.
  • Context switch events are more likely to occur, but the amount of PHP code execution time is much longer than the number of comments ignored.
  • The interpreter must maintain state, so any state service is not included during processing. What for? Because, as soon as we know that we're in a comment, we just keep reading until we find the end of the comment. All O (1) per character, unless comment begins / ends.
  • PHP installation / shutdown is ignored, since comment detection will occur regardless of existing or missing comments. The absence of comments in the code does not prevent the interpreter from trying to detect comments. Therefore you cannot slip away.

Comment your code!

+6
source

Not. It does not affect performance, but it will affect your readability. A wise man once said: "Comment, as if the next guy to save your code is a killer maniac who knows where you live."

+3
source

Minimizing javascript is a good idea, because all the code is sent to the client - for code with a lot of comments or clients with slow connections, this can lead to a significant delay in processing (where significant can mean seconds ..).

This is not the case with PHP - the code is executed directly on the server, and therefore only the processed result is sent to the client. There may be a little overhead while the PHP preprocessor parses the code, but I expect it to be sloppy - just a few milliseconds.

+1
source

Adding documentation to your code will not affect your performance in any way (either negatively or positively). Documenting your code is really important so that other people (and themselves after a few months) can figure out what is happening. It might take a lexer maybe one millisecond of a second, but it's hardly worth mentioning.

+1
source

It will increase the size of your file. But the analyzer ignores it, so it will not affect performance in terms of runtime, etc.

I think the general consensus is in the comments within the framework of real code. Although I believe that you could support separate documentation files with links to your code, I can only imagine how cumbersome it will support.

0
source

Definitely, no doubt, document your PHP code. This is already bad enough for someone who is trying to understand what you have done. Any damage, and I can’t come up with, documenting the code completely outweighs the benefits

I bet that deleting comments from almost any code leads to a noticeable zero speed improvement.

So, the answer to your "best practice": do not delete comments

0
source

The main difference is that JavaScript must be sent to the client, so these extra bytes take up resources during transmission. And it’s not very important to send documentation to the client. In server-side PHP, these comments will be ignored by the parser. So document your heartfelt content :)

0
source

the analyzer will ignore it. keep the documentation there and never skimp on it. the more comments the better.

0
source

When using docblocks to comment on your classes / functions / variables, etc., this allows IDEs that support sentence / code allocation to provide you with information about the resources you use when writing. In addition, when it comes to creating documentation, there are various tools to automate this process using docblocks; In short, if you comment correctly, you have already created your documentation.

In terms of efficiency, yes, the parser will have to filter out comments when loading a file into memory (along with a space that I could add). However, this filtering process starts only once on the server and before launching the php script itself, which itself will take significantly more time to run. This means that the proportion of time spent executing the expected PHP will be much less, and therefore any decrease in efficiency will be negligible.

Also think about the time when you will save your successors; that's enough reason;)

0
source

Source: https://habr.com/ru/post/1313802/


All Articles