Single quoted vs double quoted string

by Rekha 2009-11-13 15:04:31

Use single quoted strings unless you need to interpolate variables into your string. This saves PHP the time to scan the string for contained variables and saves about 50% execution time.


<?php
function getmicrotime($t) {

list($usec, $sec) = explode(" ",$t);

return ((float)$usec + (float)$sec);

}
$start = microtime();

$a = array();

for($i=0;$i<100;$i++) {

array_push($a, "Aaron rules!");

}
$end = microtime();

$t1 = (getmicrotime($end) - getmicrotime($start));

echo "
double quotes: $t1
";
unset($a);
$start = microtime();

$b = array();

for($i=0;$i<100;$i++) {

array_push($b, 'Aaron rules!');

}
$end = microtime();

$t2 = (getmicrotime($end) - getmicrotime($start));

$t3 = $t1-$t2;

echo "single quotes: $t2
difference...: $t3
";

?>


Results:

double quotes: 0.001505970954895
single quotes .: 0.00078308582305908
difference........: 0.00072288513183594

Tagged in:

1085
like
0
dislike
0
mail
flag

You must LOGIN to add comments