下面的示例将在更短的时间内执行 PHP 吗?如果是 A 还是 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
------------------------------------------------- */
还是用这个?
案例 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。
你的结果呢?