阅读包括文件减慢PHP脚本加载? [英] Does reading include files slow down php script load?

查看:137
本文介绍了阅读包括文件减慢PHP脚本加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 include 文件是很常见的。我认为这是过度使用,保持代码整洁,而不考虑性能。对于几个 include s,磁盘应该读取这些文件,而且我们非常使用磁盘,这可能是一个缓慢的过程。但是,这不是主要的缓慢过程或速度限制过程,因为使用 file_get_contents 加载文件的速度要快几倍。



我认为这是主要的网站把JavaScript的HTML文件,而不是通过文件加载他们的原因。或者,把一个大的JS文件分成几个小的JS文件是一个好主意,因为并行http请求可以更快地加载整个JS代码。但是这和php文件是不一样的,因为php脚本在这个过程中一个接一个地读取 include 文件。


  1. 请评论这个问题有多严重?想象一下,一个网页是在0.60s加载的,可以包括的10 php文件将其变为0.70s?

  2. 尽管这种影响应该可以忽略不计,但我想知道是否有办法加快这一进程。我不是说php缓存就像 APC


<这个问题不是针对实际应用(一个典型的例子),而是一般的理论考虑。

解决方案

include ,它是一种必然。它与Java和Python中的 import 相似,因为它用于类和函数的定义。 include 应该是非常快的,但是使用它会延迟脚本执行的时间,如果它不在那里。 include 与 file_get_contents()完全不同。后者是一个函数而不是一个构造,并返回一个字符串。 include 会实际执行包含文件的代码。



当脚本下载时,关于分割JS文件的声明不正确从相同的域块并行下载,一般建议一般尽可能少包括。

我非常怀疑有多个 include s,假设所有的都是必要的,会降低你的页面性能。如果您遇到性能问题,请到其他地方查看。



如果您想加快php的速度,请使用php编译器。

It is very common to use include files. I think it is overused to keep the codes tidy without considering performance. For several includes, disk should read the files, and as we extremely use disk, it can be a slow process. However, this is not the main slow process or the rate-limiting process, as loading the file with file_get_contents is few times faster.

I think this is the reason that major websites put javascripts within the html file rather than loading them by file. Alternatively, it can be a good idea to split a large JS file into several small JS files, as parallel http requests can load the entire JS codes faster. But this is different from php files, as php script reads include files one by one during the process.

  1. Please comment how much serious this problem can be? Imagine a webpage is loaded in 0.60s, can include of 10 php files turn it to 0.70s?

  2. Although this effect should be negligible, I want to know if there are approaches to speed up this process. I do not mean php caching like APC.

P.S. This question is not for practical application (a typical case), but theoretical considerations in general.

解决方案

include and its ilk is a necessity. It is similar to import in Java and python in that it is used for class and function definitions. include should be extremely fast, but using it will delay script execution compared to if it was not there. include is totally different from file_get_contents(). The latter is a function rather than a construct and returns a string. include will actually execute the code of the included file.

Your statement about splitting JS files is incorrect as script downloads from the same domain block parallel downloads and it's generally recommended to have as few includes as possible in general.

I highly doubt that having multiple includes, assuming all are necessary, is going to slow down the performance of your page. If you are having performance problems, look elsewhere.

If you want to speed up php, look into using a php compiler.

这篇关于阅读包括文件减慢PHP脚本加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆