以下示例中的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.
你的结果怎么样?
答案 0 :(得分:1)
评论在lexing时间消除,因此其内容无关紧要,尤其是在您的基准测试中。
如果文件中的两个多行注释总共具有相同的字节数,则它们对PHP具有完全相同的效果。一个较大的评论会花费更多的时间来完整处理,然后丢弃,但是,我们仍然在讨论lexing阶段,这个阶段非常快,你需要一个评论大小与几个字节的千兆字节注释注意到差异。
如果您使用操作码缓存(或者只是将PHP 5.5+作为Apache模块或FCGI运行,那里现在有内置的操作码缓存),您将看到零差异,因为操作码缓存的想法是使它只进行一次lexing和解析。
如果你坚持做一个测试,至少从外面做 - 用以下内容创建一个文件:
<?php
$start = microtime(true);
include 'test.php';
echo microtime(true) - $start;
并替换&#39; test.php&#39;以及您要测试的任何文件的名称。确保每次运行每个测试文件,因为无论如何差异都是微不足道的。为了使差异更明显,您可能希望为该文件生成数百万条注释。这是一个示例生成器:
<?php
$file = '<?php';
for ($i=0; $i<1000000; $i++) {
$file .= "/* comment */\n";
}
$file .= '?>';
file_put_contents('test.php', $file);
您不应该看到与大致相同字节数的注释存在任何统计上显着的差异。
答案 1 :(得分:1)
最终结果: DOCBLOCK 获胜。
比最慢的CASE A快8 ms。数百万条评论的Docblock大约需要237 ms。
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万条评论,则CASE B比CASE A快。 案例B 平均3毫秒更快。
比案例B和C更快:
我已经用这个新的测试了:
/* comment */
0.27404689788818
0.27441191673279
0.27490782737732
因此,在我的项目中,永远不会像CASE A或B和C那样。
最后D, DOCBLOCK :
最后, DOCBLOCK 最快:
/**
* comment
*/
[NL]
0.23765897750854