PHP Scripting Performance Tips
This is a collection of how to write the fastest (execution wise) performing PHP scripts. Feel free to add your share.
Only type cast when absolutely required
Type-casting slows down PHP!<?php
$a = null;
$b = null;
$c = null;
$max = 1e6;
$start = microtime(true);
for ($i = 0; $i < $max; $i++)
{
$a = 1234+5678+$i;
$b = 1234*5678+$i;
$c = 1234/2+$i;
}
var_dump(microtime(true) - $start);
$start = microtime(true);
for ($i = 0; $i < $max; $i++)
{
$a = (int)1234+(int)5678+$i;
$b = (int)1234*(int)5678+$i;
$c = (int)1234/(int)2+$i;
}
var_dump(microtime(true) - $start);
Result
No type casting: float(0.23545098304749)
With type casting: float(0.35851693153381)Never use double quotes unless absolutely required
PHP will try to evaluate variables!<?php
$max = 1e6;
ob_start();
$start = microtime(true);
for ($i = 0; $i < $max; $i++)
{
echo "microyahoodleple!!!~ ".$i;
}
ob_end_clean();
var_dump(microtime(true) - $start);
ob_start();
$start = microtime(true);
for ($i = 0; $i < $max; $i++)
{
echo 'microyahoodleple!!!~ '.$i;
}
ob_end_clean();
var_dump(microtime(true) - $start);
ob_start();
$start = microtime(true);
for ($i = 0; $i < $max; $i++)
{
echo "microyahoodleple!!!~ $i";
}
ob_end_clean();
var_dump(microtime(true) - $start);
Result
Double quotes eval + concatenation: float(0.40142297744751)
Concatenation: float(0.27414393424988)
Double quotes eval: float(0.33820700645447)
Version
0.1
Last Updated
30th September, 2011
First Published
30th September, 2011
Comments
Leave a comment
Any published comments will adhere to the etiquette policy and all e-mail addresses will be treated in accordance to the privacy policy.
Me in real-time
- If you see this, something's wrong with Twitter.
Blatant Plug
Support
I've pledged my support to these organisations, and I think you should too.






