PHP include():文件大小&性能 [英] PHP include(): File size & performance

查看:125
本文介绍了PHP include():文件大小&性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

缺乏经验的PHP问题:

An inexperienced PHP question:

我有一个PHP脚本文件,我需要在很多地方多次包含在不同的页面上。

I've got a PHP script file that I need to include on different pages lots of times in lots of places.

我可以选择将包含的文件分解为几个较小的文件,并根据需要包含这些文件......或者......我可以将它们全部保存在一起在一个PHP文件中。

I have the option of either breaking the included file down into several smaller files and include these on a as-needed basis... OR ... I could just keep it all together in a single PHP file.

我想知道在这种情况下是否对include()使用更大的文件和更小的文件有什么性能影响?例如,200KB文件和20KB文件之间是否有任何性能差异?

I'm wondering if there's any performance impact of using a larger vs. smaller file for include() in this context? For example, is there any performance difference between a 200KB file and a 20KB file?

谢谢。

推荐答案

在200KB到20KB的文件之间会有区别......但你可能不会注意到它:200KB的文件不是那么大 - 你通常会使用很多文件当你构建一个大型应用程序时,文件不是小。

There will be a difference, between a 200KB and a 20KB file... But you will probably not notice it : a 200KB file is not that big -- and you generally use a lot of files that are not "small", when you're building a big application.

当你加载一个<$ c $时,有两件事需要时间c> .php file:

There are two things that take time, when you're loading a .php file :


  • PHP源代码被编译为opcodes - 这相当于JAVA字节码


    • 每次包含PHP文件时都会这样做,默认

    • 但是,使用像 APC 这样的操作码缓存,这些操作码可以保存在内存中,这个编译的东西每次都没有完成 - 这很棒:它意味着更少的CPU使用,因为编译将会不再做了(它只会在一段时间内完成)

    • The PHP source code is "compiled" to "opcodes" -- that's quite equivalent to JAVA bytecode
      • This is done each time a PHP file is included, by default
      • But, using some opcode cache like APC, those opcodes can be kept in memory, and this compilation stuff not done each time anymore -- which is great : it'll mean less CPU used, as the compilation will not be done anymore (it'll be done only once in a while).

      • 根据脚本的内容,这可能需要一些时间,或者不需要:

      • 如果只是文件包含函数或类定义,这不会花费太多时间:什么都不会执行。

      • 如果文件包含说明,则需要更多时间^^



      作为一个注释:在一般情况下,你会获得很多更多的时间/ CPU /资源优化您的SQL查询,或添加一些缓存机制,而不是考虑那种东西。


      As a sidnote : in a general situation, you'll gain a lot more time/cpu/resources optimizing your SQL queries, or adding some caching mecanism, than thinking about that kind of stuff.

      这篇关于PHP include():文件大小&amp;性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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