note 102346 deleted from language.operators.string by thiago
| From: | thiago@php.net | Date: | Thu, 10 Feb 2011 12:56:51 +0000 |
| Subject: | note 102346 deleted from language.operators.string by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176851@lists.php.net to get a copy of this message | ||
Note Submitter: frank at interactinet dot com
----
using single quotes and concatenation can be faster than using double quotes.
<?php
$y = 'this is a test';
$x = "$y to see how concat compares";
// double quote test average for 10000 iterations: 0.001625 sec
$x = $y.' to see how concat compares';
// single quote plus concat test average: 0.001522 sec
?>
The concatenation was approx 7% faster.
Surprisingly, with more variables the concatenation becomes even better at 35% faster:
<?php
$x = "$y to see how concat compares $y";
// 0.002456641 sec average
$x = $y.' to see how concat compares'.$y;
// 0.00181675 sec average
?>
So, it is better to create strings with single quotes.
Here was the benchmark code:
<?php
for($j=0;$j<10;$j++)
{
$s = microtime(TRUE);
for($i=0;$i<10000;$i++)
{
$x = "$y to see how concat compares $y";
}
echo microtime(TRUE)-$s,'<br>';
}
echo '<br><br><br>';
for($j=0;$j<10;$j++)
{
$s = microtime(TRUE);
for($i=0;$i<10000;$i++)
{
$x = $y.' to see how concat compares'.$y;
}
echo microtime(TRUE)-$s,'<br>';
}
exit();
?>