PHP 中的定义效率如何?
C++ 预处理器#define
完全不同。
PHP define()
与仅创建 var 有什么不同吗?
define("SETTING", 0);
$something = SETTING;
与
$setting = 0;
$something = $setting;
C++ preprocessor #define
is totally different.
Is the PHP define()
any different than just creating a var?
define("SETTING", 0);
$something = SETTING;
vs
$setting = 0;
$something = $setting;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
一般来说,常量的概念是在您的程序中保持常量(听起来很有趣,对吧?;))。 这意味着编译器(解释器)将在整个脚本中将“FOOBAR”替换为 FOOBAR 的值。
理论和优点就这么多——如果你编译的话。 现在 PHP 非常动态,在大多数情况下您不会注意到不同,因为 PHP 脚本在每次运行时都会编译。 我可以告诉您,除非您使用字节码缓存,例如 ,否则您不应该看到常量和变量之间的速度存在显着差异APC、Zend 优化器 或 eAccelerator。 那么它就有意义了。
常量的所有其他优点/缺点已在此处注明,并且可以在 PHP 手册中找到。
In general, the idea of a constant is to be constant, (Sounds funny, right? ;)) inside your program. Which means that the compiler (interpreter) will replace "FOOBAR" with FOOBAR's value throughout your entire script.
So much for the theory and the advantages - if you compile. Now PHP is pretty dynamic and in most cases you will not notice a different because the PHP script is compiled with each run. Afai-can-tell you should not see a notable difference in speed between constants and variables unless you use a byte-code cache such as APC, Zend Optimizer or eAccelerator. Then it can make sense.
All other advantages/disadvantages of constants have been already noted here and can be found in the PHP manual.
0.00689506530762
0.00941896438599
这是可重复的,结果相似。 在我看来,常量的定义和/或使用比变量要慢一些。
0.00689506530762
0.00941896438599
This is repeatable with similar results. It looks to me like constants are a bit slower to define and/or use than variables.
以下是差异,来自 手册
对我来说,主要的好处是全球范围。 我当然不担心它们的效率——只要你需要一个不应该改变的全局标量值,就可以使用它们。
Here are the differences, from the manual
For me, the main benefit is the global scope. I certainly don't worry about their efficiency - use them whenever you need a global scalar value which should not be alterable.
看起来效率不高。 (我的所有假设都是基于 php.net 的一条评论,我自己还没有进行基准测试。)
调用一个常量,将花费调用变量时间的 2 倍。
检查常量是否存在需要 2 毫秒,误报则需要 12 毫秒!
这是来自 php 在线文档中定义页面注释的基准。
来自 tris+php at tfconsulting dot com dot au 26-Mar-2009 06:40
http://us.php.net/manual/en/function.define.php#89886
NOT efficient it appears. (And i'm basing all the assumptions here on one comment from php.net, i still haven't did the benchmarks myself.)
recalling a constant, will take 2x the time of recalling a variable.
checking the existence of a Constant will take 2ms and 12ms for a false positive!
Here's a benchmark from the comments of the define page in php's online doc.
from tris+php at tfconsulting dot com dot au 26-Mar-2009 06:40
http://us.php.net/manual/en/function.defined.php#89886
Define是简单的静态意义,意味着它的值在运行时不能改变,而变量是动态意义,因为你可以在过程中自由地操纵它的值。
Define is simple static sense, meaning its value can't be changed during runtime while variable is dynamic sense because you can freely manipulate its value along the process.
当我运行速度测试时,设置和转储常量的运行速度比设置变量并转储它们的速度要快得多。
When I run speed tests, constants being set and dumped out run much a little faster than setting variables and dumping them out.
2020 更新(PHP 7.2、AMD Ryzen9、启用 Zend OpCache)
代码:
输出:
2020 update (PHP 7.2, AMD Ryzen9, Zend OpCache enabled)
CODE:
OUTPUT:
主要区别:
Main differences:
不确定效率,但它不仅仅是创建一个 var:
Not sure about efficiency, but it is more than creating a var:
“定义”操作本身相当慢 - 由 xdebug 分析器确认。
这是来自 http://t3.dotgnu.info/blog 的基准/php/my-first-php-extension.html:
纯“定义”
380.785 次读取/秒
14.2647 平均毫秒/第一响应
用“hidef”扩展定义的常数
930.783 次读取/秒
6.30279 平均毫秒/第一响应
损坏的链接更新
上面引用的博客文章已离开互联网。 仍然可以查看 此处通过 Wayback Machine。 这是另一篇类似文章。
作者引用的库可以在此处 (apc_define_constants) 和此处(hidef 扩展)。
'define' operation itself is rather slow - confirmed by xdebug profiler.
Here is benchmarks from http://t3.dotgnu.info/blog/php/my-first-php-extension.html:
pure 'define'
380.785 fetches/sec
14.2647 mean msecs/first-response
constants defined with 'hidef' extension
930.783 fetches/sec
6.30279 mean msecs/first-response
broken link update
The blog post referenced above has left the internet. It can still be viewed here via Wayback Machine. Here is another similar article.
The libraries the author references can be found here (apc_define_constants) and here (hidef extension).