All pure statements, don't believe me? try yourself.$i++;
is slower than ++$i;
$i += 1;
is slower than++$i;
rand($min,$max);
is slower thanmt_rand($min,$max);
$i = 0; while($array[$i]){ ++$i; }
is slower than
$i = 0; $count = count($array); while($i < $count){ ++$i; }
print 'anything';
is slower thanecho 'anything';
echo 'anything (pure HTML, in php tags)';
is slower than
anything (pure HTML, not in php tags)
include('output/contain_no_php');
is slower thanreadfile('output/contain_no_php');
proof.
mysql_fetch_array($result);
is not as smart asmysql_fetch_assoc($result);
echo "$variable string";
is slower thanecho $variable.'string';
echo $variable.'string';
is slower thanecho $variable,'string';
A_CONSTANT;
$a_variable;
I don't know which one is faster... different test have different results
constant('A_CONSTANT');
is slower thanA_CONSTANT;
pi();
is slower thanM_PI;
if($number<0){ $number *= -1; }
is slower thanabs($number);
is slower than
($x + 1) * pow($x , $exp - 1); $a*($b+$c+$d);
Expanded form are usually slower than factorized form.
if($a == $b)
is slower thanif($a === $b)
pow($x, 0.5);
is slower thansqrt($x);
1/2;
is slower than0.5;
function some_function($var){ global $newvar; }
is slower than
function some_function($var){ return $newvar; }
file($filename);
is not as smart as
sha1('anystring');
is slower thanmd5('anystring');
array_rand($array);
is slower than
if($a == 30);
is less smart thanif(30 == $a);
if($i != 0);
is slower thanif($i);
$_SERVER["PHP_SELF"];
is slower than__FILE__;
2nd one is proven faster, here is the article.
$a = 3*4;
is slower than$a = 3 << 2;
while($i < count($array)){ ++$i; }
is slower than
$count = count($array); while($i < $count){ ++$i; }
$array[string];
is slower than$array['string'];
preg_match("![0-9]+!", $foo);
is slower thanctype_digit($foo);
This is from Ilia Alshanetsky's 12 PHP Optimization Tricks
$a = TRUE;
is slower than$a = true;
for, while loops is slower than do-while loops
do-while loop is the fastest loop ever. For VS While loop benchmark. While VS Do-while in here
I suggest you read Ilia Alshanetsky's zend performance slides and PHP Presents on Performance if you want more optimization knowledge.
评论