PHP操作码缓存的基础 [英] Basics of PHP opcode cache

查看:161
本文介绍了PHP操作码缓存的基础的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,在一个我不打算将其再次用于另一个站点的大型项目中,我已经将该站点的名称硬编码到了所有文件中.现在,如果我要更改站点名称,则需要花费很多精力才能在任何地方进行更改.我知道一个明显的解决方案是将名称存储为变量或常量,但是我想您可以将其称为我的微优化思维方式:我一直认为这将是PHP解析起来少一件事.我确实意识到这不会有太大的不同,但是我只是想知道使用像APC这样的操作码缓存是否意味着PHP甚至不必重新解析它?

Currently on a very large project that I do not plan to re-use for another site, I have the site's name hardcoded into the files everywhere. Now, if I were ever to change the site name it would take a lot of effort to change that everywhere. I know the obvious solution is to just store the name as a variable or a constant, but I guess you could call it my micro-optimizing way of thinking: I always figured it would be one less thing PHP has to parse. I do realize it won't make much difference, but I just wanted to know whether using an opcode cache like APC would mean that PHP wouldn't even have to re-parse that?

推荐答案

真的:您不应该在意这样的事情.

Really : you shouldn't care about anything like that.

任何配置上的差异都意味着很多上的差异(例如,对于APC,apc.stat选项可能会对服务器的负载-以及其他任何方面产生很大的影响像数据库查询一样,您将获得数百倍的影响)

Any difference in configuration will means much more difference (for instance, the apc.stat option, for APC, can have quite an impact on the load of your server -- and anything like DB queries you do will have hundreds of time more impact)

在这里,可能重要的是可维护性:

Here, what probably matters is maintenability :

  • 使用非硬编码的站点名称是否对您有好处((纳米优化除外))?
  • 对它进行硬编码(相同的例外) 是否对您有任何好处?
  • does it get you any benefit (except from that nano-optimisation) to have the site name not hard-coded ?
  • does it get you any benefit to have it hard-coded (same exception) ?

如果在两种情况下答案都是否",并且您的应用程序正常工作……那很重要!

If the answer is "no" in either case, and your application works... well, that's what matters !


如果您有时间花在进行微优化上,而不是进行微优化,那最好是用探查器遍历您的应用程序代码,遍历数据库查询,您正在获取静态JS/CSS的HTTP请求数/images,升级PHP或修改您的代码,使其可以在PHP 5.3 上运行(因为PHP 5.3对5.2进行了一些优化),...


If you have time to spend with that kind of less than micro optimisations, it would probably be better spent going through your application code with a profiler, going through your DB queries, the number of HTTP requests you are doing to fetch static JS/CSS/images, upgrading PHP or modifying your code so it can run on PHP 5.3 (as PHP 5.3 comes with some optimisations over 5.2), ...

所有这些最有可能为您带来更高的收益;-)

All those will most probably get you a higher gain ;-)


在评论后进行

基本上,在加载PHP文件时:

Basically, when a PHP file is loaded :

  • 从磁盘读取文件
  • 已被解析并编译为操作码
  • 操作码已执行

具有操作码缓存:

  • 如果RAM中有一个包含操作码的地方,则这些操作码是从RAM中加载的(即不读取文件,也不进行解析/编译)
    • 如果没有,请参见之前的步骤-对于下一个请求,只需在执行之前添加将操作码存储到RAM"
    • if there is a place in RAM containing the opcodes, those are loaded from RAM (is, no reading of a file, nor parsing/compiling)
      • if not, see the steps before -- just add a "store the opcodes to RAM" before execution, for the next request

      apc.stat选项定义APC是应该检查文件的最后修改日期/时间,以决定是使用RAM中的操作码,还是重新编译文件(如果RAM中的操作码是较新的文件).

      The apc.stat option defines whether APC should examine the last modification date/time of a file to decide between using the opcodes from RAM, or re-compiling the file if it is more recent that the opcodes in RAM.

      禁用此选项意味着:

      • 不检查磁盘上的文件=>更快,并且使用更少的资源
        • 例如,当在负载非常大的服务器上禁用此选项时,我发现CPU负载下降了10%至15%.
        • files are not checked on disk => faster, and uses less resources
          • For instance, I've seen a drop of CPU-load, between 10 and 15%, when disabling this option on a quite loaded server


          不过,我所说的是正确的:可能有很多可以优化的事情,比起简单的我应该使用硬编码的值"与我应该使用常量/变量",这意味着更重要的收益.


          Still, what I said is true : there are probably lots of things you can optimise that will mean more important gain than a simple "should I use hard-coded values" versus "should I use constants/variables".

          这篇关于PHP操作码缓存的基础的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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