When is the .php file too large?

I am writing a PHP class. There are currently 1000 lines of code, and the file size is 46 KB. This is not a crazy size, but it made me think.

Q: At what point will performance be affected due to file size and / or the amount of code included in the PHP file?

+4
source share
3 answers

There is no noticeable difference for php (especially if you use some kind of operation cache code) if there are 10 files every 100 lines or 1 with 1000 lines.

But in terms of serviceability, it would be better to share class responsibilities with several.

+5
source

Like other people, the focus on file size should be in terms of readability and code understanding. [/ p>

However, regarding your initial question about the size and performance of a PHP file, it depends on what type of performance is needed. For PHP code that runs infrequently โ€” for example, a PHP program that runs on the client machine through the command line, rather than visitors downloading a web page โ€” large file sizes may not be displayed.

Unlike high-performance scripts, even a fairly small PHP file will consume significant system resources. A common PHP performance issue is what prompted Facebook to write its own JIT virtual machine to execute PHP code - because the once acceptable performance with PHP became horrific when Facebook expanded.

The above is true for optimization in general - it is difficult to draw a line between good and bad performance without having a wider context for classifying internally. If you are concerned about the performance of your PHP code - whether the file size is related or not, I recommend using a PHP profiler like Xdebug and monitoring system resources on your server.

EDIT, as suggested by Sรฉbastien Renauld , I am adding to my answer the following information about PHP and caching of operation code. However, instead of writing all this, I would like to point out the accepted answer to this question fooobar.com/questions/149315 / ...

+2
source

I am not an expert about anything, but I will say that you probably will not run into problems until the size of your file becomes a significant part of your PHP memory limit.

And I will tell you, I have a mess of code in one PHP file. I just checked and this is 11,487 lines.

This file, and another - 2056 lines, are included on each page of my development site.

I do not see any obvious performance issues.

Perhaps I should explain why I have such a large file.

I am focused on making the whole program work. And I decided to just use this file for most classes so that I can easily search for it and make changes to any class without looking for the file. After I finished, I divided all the classes into separate files and used autoload to load only the necessary classes. It probably won't be for a few weeks, but when I do, I will do some benchmarking before and after to see if there is a difference in performance, but I doubt it will.

Currently, every page of my site is loading this file. I just loaded the main page, and according to Chrome, it took 102 ms. The page, which actually uses many classes and does some data intensive work and interacts with MySQL, took 279 ms.

So, it seems to me that the file size does not become important until it becomes much larger than my 11,487 lines. For speed, you need to worry more about optimizing your code.

For example, I just asked a question about processing arrays, in which some solutions had my code running for more than 5 minutes, about 30 seconds, and a couple of solutions in about 280 ms. Do not worry about file size!

0
source

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


All Articles