PHP include vs include_once(速度)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4326435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:41:18  来源:igfitidea点击:

PHP include vs include_once (speed)

php

提问by Ricket

Possible Duplicate:
Why is require_once so bad to use?

可能的重复:
为什么 require_once 使用起来如此糟糕?

I've read somewhere that the include_onceand require_oncestatements in PHP were slower than their non-once counterparts. Is this a significant slowdown? Has there been any testing or studies of this, and has it changed in recent versions of PHP?

我在某处读到PHP 中的include_onceandrequire_once语句比它们的 non-once 语句慢。这是一个显着的放缓吗?是否对此进行了任何测试或研究,并且在最新版本的 PHP 中是否发生了变化?

回答by SW4

The speed increase is minimal and comes as a reference check is conducted to prevent code duplication. The 'once' appendage is a preventative measure against the same code being executed/included twice..this performing this check comes at a minor speed cost.

速度增加很小,并且进行了参考检查以防止代码重复。'once' appendage 是针对同一代码被执行/包含两次的预防措施......执行此检查的速度成本很小。

If there is ever an instance where you are using _oncelook into why it is the case, is you code really built in the most efficient way? It is often better to remove the need to rely on _onceand produce better code (easier said than done!).

如果曾经有一个您正在使用的实例,请_once查看为什么会出现这种情况,您的代码是否真的以最有效的方式构建?消除对依赖的需求_once并生成更好的代码通常更好(说起来容易做起来难!)。

See:

看:

http://forums.digitalpoint.com/showthread.php?t=1693837

http://forums.digitalpoint.com/showthread.php?t=1693837

http://www.phpbb.com/community/viewtopic.php?f=71&t=565933

http://www.phpbb.com/community/viewtopic.php?f=71&t=565933

http://www.sitepoint.com/forums/showthread.php?t=269085

http://www.sitepoint.com/forums/showthread.php?t=269085

http://www.quora.com/What-is-the-difference-between-functions-include-and-include_once-in-PHP

http://www.quora.com/What-is-the-difference-between-functions-include-and-include_once-in-PHP

回答by Nicolas

The include_once and require_once functions are slower than include and require, simply because they keep track of the files that have already been included, to avoid including them more than once.

include_once 和 require_once 函数比 include 和 require 慢,因为它们跟踪已经包含的文件,以避免多次包含它们。

But that shouldn't matter AT ALL, since there are probably many ways to optimize your application, way more efficient than this one.

但这根本不重要,因为可能有很多方法可以优化您的应用程序,比这一种更有效。