note 106485 added to language.operators.string
| From: | deniska666 at mail dot ru | Date: | Sun, 13 Nov 2011 16:20:33 +0000 |
| Subject: | note 106485 added to language.operators.string | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-184416@lists.php.net to get a copy of this message | ||
kevin at metalaxe dot com wrote about concatenation vs embedding. I've tested it on 5.3.8, and
got completely different results:
<?php
class Timer {
private $starttime;
private $lasttime;
public function __construct() {
;
}
public function init() {
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$this->starttime = $mtime;
$this->lasttime = $mtime;
}
public function getLastTimeDiff() {
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$timediff = $mtime - $this->lasttime;
$this->lasttime = $mtime;
return $timediff;
}
}
$timer = new Timer();
$var = "1";
for( $x=0; $x < 101; $x++ )
{
echo "Step: $x\n";
$timer->init();
for( $i=0; $i<100001; $i++ )
{
$string = " {$var} {$var} {$var} {$var} {$var} {$var} {$var} {$var} {$var} {$var}
{$var} {$var} {$var} {$var} {$var}";
unset( $string );
}
echo "15 embedded vars: ".$timer->getLastTimeDiff()." seconds\n";.
$timer->init();
for( $i=0; $i<100001; $i++ )
{
$string = ' '.$var.' '.$var.' '.$var.' '.$var.'
'.$var.' '.$var.' '.$var.' '.$var.' '.$var.'
'.$var.' '.$var.' '.$var.' '.$var.' '.$var.'
'.$var;
unset( $string );
}
echo "15 concatenations: ".$timer->getLastTimeDiff()." seconds\n";.
}
?>
Gives me more than twice faster embedding than concatenation:
15 embedded vars: 0.11846089363098 seconds
15 concatenations: 0.24820780754089 seconds
And please note that I had included 1 in quotes. It will take twice more time to concatenate/embed
if php needs to cast before every operation. So it is reasonable to do it once before
concatenations/embeddings.
----
Server IP: 69.147.83.197
Probable Submitter: 62.24.87.69
----
Manual Page -- http://www.php.net/manual/en/language.operators.string.php
Edit -- https://master.php.net/note/edit/106485
Del: integrated -- https://master.php.net/note/delete/106485/integrated
Del: useless -- https://master.php.net/note/delete/106485/useless
Del: bad code -- https://master.php.net/note/delete/106485/bad+code
Del: spam -- https://master.php.net/note/delete/106485/spam
Del: non-english -- https://master.php.net/note/delete/106485/non-english
Del: in docs -- https://master.php.net/note/delete/106485/in+docs
Del: other reasons-- https://master.php.net/note/delete/106485
Reject -- https://master.php.net/note/reject/106485
Search -- https://master.php.net/manage/user-notes.php