note 106485 deleted from language.operators.string by danbrown

From: Date: Tue, 15 Nov 2011 16:29:35 +0000
Subject: note 106485 deleted from language.operators.string by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-184479@lists.php.net to get a copy of this message
Note Submitter: deniska666 at mail dot ru ---- 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.

« previous php.notes (#184479) next »