How big of an impact does including PHP files have on performance?

百般思念 提交于 2019-12-13 13:31:07

问题


Question pretty much states it all, I am working on a large project where most calls to php include() between 100 and 150 files. On average the time php takes is between 150 and 300 ms. I'm wondering how much of this is due to including PHP scripts? I've been thinking about running a script that checks most accessed files for particular calls and merge them into one file to speed things up, but for all I know this has zero impact.

I should note that I use APC, I'm not fully aware of what APC does in the background, but I would imagine it might already cache my files somehow so the amount of files doens't really make a big difference?

Would appreciate any input on the subject.

Of course, 300ms isnt much, but if I can bring it down to say, 100 or even 50ms, thats a significant boost.

Edit:

To clarify I am talking about file loading by php include / require.


回答1:


File loading is a tricky thing. As others have said, the only sure fire way to tell is to do some benchmarks. However, here are some general rules that apply only to PHP loading, not files with fopen:

  • APC will store its opcode cache in shared memory so you will take a hit on the first load but not subsequent loads.
  • include and include_once (and their require cousins) are actually quite heavy. Here are some tips to improve their speed:
    • Use absolute paths to your files (avoid relative paths like ../foo.php)
    • Both the _once functions need to check to make sure that the file wasn't also included via a symbolic link since a symbolic link can produce multiple paths to the same file. This is extremely expensive. (see next point)
  • It is much cheaper to load only the files you need than to call include. Make use of auto-loaders to only load classes when they are needed.
  • Local disks will almost always be a better bet than networked storage. When possible, if you have multiple servers, keep copies of the source code on each server. It means you need to update multiple places during a release but it is worth the effort in performance.

Overall it is dependent on your hard disk speed. But compared to not loading a file at all or loading it from RAM, file loading is incredible slow.

I hope that helped.




回答2:


That is quite a bit of files, but not to be unexpected if using a framework (Zend by any chance?). The impact of including that many files is mainly dependent on your server's hard drives' speed. Regardless, file access is extremely slow so if you can, reduce the number of includes.

APC does/can cache the opcodes for all those files in memory though, meaning no more disk seeks until the cache is invalidated/destroyed.

  • Try turning APC off and see how much of a difference it makes. There should be a noticeable spike in execution time.

  • Try profiling the script with xdebug. You'll most likely find that there are other issues (code issues) that affect performance more than the file access.



来源:https://stackoverflow.com/questions/6359970/how-big-of-an-impact-does-including-php-files-have-on-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!