php - 哪一个代码可以更快地处理 PHP 注释?有想法吗?

标签 php comments execution

下面的示例对于 PHP 来说会在更短的时间内执行什么?情况A或B?如何正确测试这个?

这样可以在短时间内更快地处理:

案例A:

/* -------------------------------------------------
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
------------------------------------------------- */

或者用这个?

案例B:

/****************************************************
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
*****************************************************/

我使用此代码进行测试:

<?php
echo '<pre>';

$s = microtime(true);
for ($i=0; $i<=10000000; $i++) {
/* -------------------------------------------------

------------------------------------------------- */
}
echo "1: ";
$r1 = microtime(true) - $s;
echo $r1;
echo "\n";

$s2 = microtime(true);
for ($i=0; $i<=10000000; $i++) {
/* -------------------------------------------------
    some information inside commenting rules
------------------------------------------------- */
}
echo "2: ";
$r2 = microtime(true) - $s2;
echo $r2;
echo "\n";

$s3 = microtime(true);
for ($i=0; $i<=10000000; $i++) {
/* -------------------------------------------------
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
------------------------------------------------- */
}
echo "3: ";
$r3 = microtime(true) - $s3;
echo $r3;
echo "\n";


$s4 = microtime(true);
for ($i=0; $i<=10000000; $i++) {
/****************************************************
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
    some information inside commenting rules
*****************************************************/
}
echo "4: ";
$r4 = microtime(true) - $s4;
echo $r4;
echo "\n";

$result = array('1 without text', $r1,
      '2 single line', $r2,
      '3 multiline separator', $r3,
      '4 multiline starred', $r4);

echo min($result);

echo '</pre>';

结果可能因执行和内存操作而异。大多数情况下,我的案例结果是 CASE B。

你的结果怎么样?

最佳答案

注释在词法分析时被消除,因此它们的内容是不相关的,尤其是在上面的基准测试中。

如果文件中的两个多行注释的总字节数相同,则它们对 PHP 的影响完全相同。较大的注释将需要更多的时间来完全处理,然后被丢弃,但是,我们仍然在谈论词法分析阶段,它的速度非常快,以至于您需要一个大小为千兆字节的注释,而不是几个大小的注释字节注释以注意到差异。

如果您使用操作码缓存(或者只是将 PHP 5.5+ 作为 Apache 模块或 FCGI 运行,其中现在有内置操作码缓存),您将看到零差异,因为操作码的想法-代码缓存是为了让词法分析和解析只完成一次。

如果您坚持进行测试,至少从外部进行 - 创建一个包含以下内容的文件:

<?php
$start = microtime(true);
include 'test.php';
echo microtime(true) - $start;

并将“test.php”替换为您要测试的任何文件的名称。确保运行每个测试文件几次,因为无论如何差异都是微不足道的。为了使差异更加明显,您可能希望为该文件生成数百万条评论。这是一个示例生成器:

<?php
$file = '<?php';
for ($i=0; $i<1000000; $i++) {
    $file .= "/* comment */\n";
}
$file .= '?>';
file_put_contents('test.php', $file);

对于字节数大致相同的评论,您不应该看到任何统计上显着的差异。

关于php - 哪一个代码可以更快地处理 PHP 注释?有想法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18242377/

相关文章:

php - 从 php 脚本调用的 INSERT INTO 失败

javascript - 从 javascript 调用不同的 PHP 函数

python - 如何在 Python 中编写内联注释

c# - 评论泛型——是否可以将泛型类型参数引用为它的实际类型而不是 T?

javascript - javascript 函数调用是否在整个脚本解析完成之前运行?

PHP:执行 php 脚本时显示 "loading"页面

php - 删除 iframe 会阻止文件上传吗?

php - 从远程托管服务器加载数据库: phpmyadmin

documentation - 忽略Doxygen注释 block 中的行

c# - 说服 C# 编译器执行将在成员返回后停止