note 102346 added to language.operators.string
| From: | frank at interactinet dot com | Date: | Wed, 09 Feb 2011 22:14:59 +0000 |
| Subject: | note 102346 added to language.operators.string | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-176827@lists.php.net to get a copy of this message | ||
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();
?>
----
Server IP: 69.147.83.197
Probable Submitter: 68.179.14.105
----
Manual Page -- http://www.php.net/manual/en/language.operators.string.php
Edit -- https://master.php.net/note/edit/102346
Del: integrated -- https://master.php.net/note/delete/102346/integrated
Del: useless -- https://master.php.net/note/delete/102346/useless
Del: bad code -- https://master.php.net/note/delete/102346/bad+code
Del: spam -- https://master.php.net/note/delete/102346/spam
Del: non-english -- https://master.php.net/note/delete/102346/non-english
Del: in docs -- https://master.php.net/note/delete/102346/in+docs
Del: other reasons-- https://master.php.net/note/delete/102346
Reject -- https://master.php.net/note/reject/102346
Search -- https://master.php.net/manage/user-notes.php