哪些代码可以更快地处理PHP注释?想法


Which of code will process faster PHP comments? Ideas?

下面的例子会让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>';

结果可能因执行和内存操作而有所不同。在大多数情况下,我的案例结果是案例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);

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

最终结果:DOCBLOCK获胜。

结果:

比最慢的CASE A快8毫秒。数百万条评论上的文档块大约需要237毫秒。

比较:

 CASE     SECONDS:    EXAMPLE CODE:
 A:       0.304       /******************** comment ********************/
 B:       0.343       /*------------------- comment ---------------------*/
 C:       0.293       /*                    comment                    */
 D:       0.237       /**
                       * comment
                       */
 [millions of comments in separated files]

测试结果:

对于情况A:

/******************** comment ********************/
0.31907296180725
0.31833505630493
0.31972694396973

对于情况B:

/*------------------- comment ---------------------*/
0.2824490070343
0.28207182884216
0.28176498413086

结果以毫秒(毫秒)为单位。我已经将执行或加载的代码排序为单独的文件,结果是相同的,所以请确保测试正确。

结果:

所以,如果我在一个文件上有100万条评论,那么案例B比案例a更快情况B平均3毫秒更快。

比情况B和C更快:

我测试了这个新的:

/*                    comment                    */
0.27404689788818
0.27441191673279
0.27490782737732

所以在我的项目中,永远不会像案例A、B和C那样。

最后D,DOCBLOCK

最后,DOCBLOCK是最快的:

/**
 * comment
 */
[NL]
0.23765897750854