PHP Speed Freaks

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 than
echo '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 than
readfile('output/contain_no_php');
proof.

mysql_fetch_array($result);
is not as smart as
mysql_fetch_assoc($result);

echo "$variable string";
is slower than
echo $variable.'string';

echo $variable.'string';
is slower than
echo $variable,'string';

A_CONSTANT;
$a_variable;
I don't know which one is faster... different test have different results

constant('A_CONSTANT');
is slower than
A_CONSTANT;

pi();
is slower than
M_PI;

if($number<0){
	$number *= -1;
}

is slower than
abs($number);

pow($x , $exp) + pow($x , $exp - 1);
$a*$b+$a*$c+$a*$d;

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 than
if($a === $b)

pow($x, 0.5);
is slower than
sqrt($x);

1/2;
is slower than
0.5;

function some_function($var){
	global $newvar;
}

is slower than

function some_function($var){
	return $newvar;
}

file($filename);
is not as smart as

$fp=fopen($filename,wb);
flock($fp,LOCK_SH);
$filedata=fread($fp,filesize($filename));
fclose($fp);

sha1('anystring');
is slower than
md5('anystring');

array_rand($array);
is slower than

$count = count($array);
mt_rand(0, $count);

if($a == 30);
is less smart than
if(30 == $a);

if($i != 0);
is slower than
if($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 than
ctype_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.

To avoid debt consolidation, make sure that you use a mortgage calculator everytime you use a credit card and whether it is car finance or some other purchase, pay cash.

Why do you say one method is

Why do you say one method is 'faster or smarter', then describe it as slower? Have you got everything backwards?

possible! I converted this

Mgccl's picture

possible!
I converted this from a table to a post, so it's likely I did something wrong so.
I will fix in a few days.

None of that made sense.

None of that made sense. Like that other guy said, you obviously lack the necessary neuron connections to use create useful information!
Faster or smarter, but slower? I hope you took E.S.L.

when I was converting it

Mgccl's picture

when I was converting it from a table to this really took me some time, and there are a lot more comment to ask me to add the reasons something is faster because people wants say... PROOF.
this page is full of info so I have problem manage it.
I'm brushing this up today.
no more proofs, just statements.

Pingback

[...] ссылку на нее в bookmark’ах, именно она и выступала в роли одного из основных источников информации для этого текста. В качестве возможных вариантов [...]

Post new comment

The content of this field is kept private and will not be shown publicly.
If you have a Gravatar account, used to display your avatar.
  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <span> <fn>
  • Lines and paragraphs break automatically.
  • Textual smileys will be replaced with graphical ones.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Use <fn>...</fn> to insert automatically numbered footnotes.

More information about formatting options

What is 53 + 4?
To combat spam, please solve the math question above.
Honey Pot that kill bots