PHP 操作码缓存/Zend 加速以及 include_once 与 require_once

发布于 2024-07-07 14:31:16 字数 294 浏览 11 评论 0 原文

我有一位同事正在为我们基于 PHP 的应用程序研究操作码缓存/Zend 加速(我一直认为它们是同一件事)。 他的基准似乎表明,如果我们使用 require_once 包含我们的(大型)类库,我们不会看到性能优势,但使用 include_once 时我们确实看到了性能优势。

这对我们俩来说都有腥味,但我自己没有时间检查我们的基准方法,而且我的同事对鱼味的容忍度比我高。 :)

有没有人遇到过这样的事情? 如果没有,对其他可能导致通过从 include_once 切换到 require_once 来提高性能的事情有什么想法吗?

I have a colleague who is looking into opcode caching/Zend Acceleration (I've always assumed these are the same thing) for our PHP based application. His Benchmarks appear to indicate that we're NOT seeing a performance benefit if we include our (large) class libraries with require_once, but we DO see the performance benefit when using include_once.

This smells fishy to both of us, but I don't have time to check into our benchmark methodology myself and my colleague has more tolerance for the smell of fish than I do. :)

Has anyone ever run into anything like this? If not, any thoughts on other things that might be causing the appearance of a performance increase by switching from include_once to require_once?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喵星人汪星人 2024-07-14 14:31:16

对于初学者来说,两个调用(require_once 和 include_once)都会仔细检查之前是否未包含文件。

因此,他们实现这一目标的方法是在所有可用路径中搜索文件,并本质上检查它是否之前没有混合过等等。

在后台发生的事情是他们评估所有不同的选项(例如多个 include_path 的等),然后通过从这个缩写形式创建真实路径,他们创建了一个唯一的标识符。 只有一条相同的路径,而不是两条。

这已经不是地球上最快的过程,并且通常在每个 PHP 请求上发生。 然后添加另一个昂贵的操作,即创建我所说的真实路径时的统计数据(真实路径,因为它类似于realpath() 会检查文件是否存在。

如果我错了,请纠正我,但 APC 特别针对这种情况进行了优化。

所以无论如何 - 现在来看看 require_once 和 include_once 之间的区别,即 require_once 在包含文件时评估文件(对于低级解析错误等)。 这是一项额外的检查,如果您有足够的 QA,解析错误永远不会潜入包含中,则可以取消该检查。

很难找到其他的。 :-)

(需要考虑的事情:您可以使用 require_once 进行开发,并在部署时用 include_once 替换所有调用。)

至于操作码缓存 - 我建议 APC。 之前在 stackoverflow 上讨论过。 就我个人而言,我/我们正在使用它一段时间(我们每天处理大约 10 万访客,有 3 个前端和 1 个后端),我们非常高兴。 APC 还针对 require_once/include_once 疯狂进行了优化。

一个非常酷的副作用是 APC 还允许您将 PHP 变量存储在内存中 - 有点持久化等。

一些额外的提示:

  1. 很多人声称可以使用 __autoload
  2. 使用操作码缓存,避免条件 require_once/include_once (例如在循环或控制流中)。
  3. 有人说 include_ 或 require_once 中的 /absolute/path/to/file.php 比依赖 include_path 更快。
  4. include_path 中路径的顺序也很重要。

希望有帮助。

For starters, both calls (require_once and include_once) double-check if a file has not been included before.

So the way they both achieve this is by searching the file in all available paths and by essentially checking if it hasn't been in the mix before etc..

In the background what happens is that they evaluate all the different options (e.g. multiple include_path's, etc.) and then by creating the realpath from this abreviated form they create a unique identifier. There is only one and the same path - not two.

This is already not the fastest process on the planet and generally happens on each request with PHP. Then add another expensive operation which is the stat when it creates what I called the realpath (realpath, because it's sort of what realpath() does) to check if the file exists.

Correct me if I am wrong, but APC has optimizations especially for this case.

So anyway - now on to the difference between require_once and include_once, which is that require_once evaluates the file (for low-level parse errors, etc.) when it includes it. This is an additional check which you can get rid of if you have enough QA in place that a parse error can never sneak into an include.

It's just tricky to find otherwise. :-)

(Something to consider: You could develop with require_once and replace all calls with include_once when you deploy.)

As for an opcode cache - I'd recommend APC. It's been discussed on stackoverflow before. Personally, I am/we are using it for a while (we handle roughly 100k visitors/day with 3 frontends and 1 backend) and we are very happy. APC is also optimized for the require_once/include_once madness.

A pretty cool side-effect is that APC also allows you to store PHP variables in memory - sort of persistant, etc..

A couple additional pointers:

  1. A lot of people claim to speed up any application with __autoload.
  2. With an opcode cache, avoid conditional require_once/include_once (e.g. in loops or in control-flow).
  3. Some people say that /absolute/path/to/file.php in include_ or require_once is faster than relying on the include_path.
  4. The order of the paths in your include_path matters as well.

Hope that helps.

青丝拂面 2024-07-14 14:31:16

我不能保证任何事情,因为我还没有足够深入地研究它,但是,是的,我已经看到了两者之间的速度差异。 不过,它们对我来说从来没有重要到足以移动到 include_once 而不是 require_once 。

我一直认为差异是因为 require_once 必须在水下做更多的工作。 至少还有一个潜在的错误需要准备和处理,并且当所需的文件不存在时还有很多工作要做。

I can't guarantuee anything as i haven't looked deeply enough into it, but yes, i have seen speed differences between the two. They were never significant enough to me to move to include_once instead of require_once though.

I always assumed the difference was because require_once has to do more work underwater. at least one more potential error to prepare for and handle, and a lot more to do when the file required does not exist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文