What will work faster? Code Example (1) $sTemp3 = $sTemp1.$sTemp2; OR Code Sample (2) $ sTemp3 = "$sTemp1$sTemp2";
Concatenating should be faster and more appropriate, but it's going to be negligible in an instance like this.
Second one (using double-quotes) is faster. The first one appends, that involves extra memory pointers. <?php $sTemp1="A"; $sTemp2="B"; // one $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; $sTemp3 = $sTemp1.$sTemp2; $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = ($endtime - $starttime); echo "Time 1 ".number_format($totaltime,10)." seconds<br/>"; unset($sTemp3); // two $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; $sTemp3 = "$sTemp1$sTemp2"; $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = ($endtime - $starttime); echo "Time 2 ".number_format($totaltime,10)." seconds<br/>"; ?> Code (markup):
Guess it depends on purpose. Yet 2nd option is about 5 times faster than the first (depending on system specs, of course), and that can matter in a lot of instances
I get option 1 is about 20% faster. You can't run the operation only once and get an accurate result. I looped 10,000 times for each operation. Time 1 0.0011868477 seconds Time 2 0.0014359951 seconds $sTemp1 = "A"; $sTemp2 = "B"; // one $mtime = microtime(); $mtime = explode(" ", $mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; for ($i = 1; $i <= 10000; $i++) { $sTemp3 = $sTemp1 . $sTemp2; } $mtime = microtime(); $mtime = explode(" ", $mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = ($endtime - $starttime); echo "Time 1 " . number_format($totaltime, 10) . " seconds<br/>"; unset($sTemp3); // two $mtime = microtime(); $mtime = explode(" ", $mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; for ($i = 1; $i <= 10000; $i++) { $sTemp3 = "$sTemp1$sTemp2"; } $mtime = microtime(); $mtime = explode(" ", $mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = ($endtime - $starttime); echo "Time 2 " . number_format($totaltime, 10) . " seconds<br/>"; PHP: From what I remember, the fastest way to join strings is, ",", followed by ".", followed by quoted text. Trying to find a reference. The comma method only works when echo'ing. This is also assuming you don't run out of memory which would be a concern when concatenating.
Fair enough. Interesting how up to about 1000 reps, they're both either equal or 2nd one's quicker. Perhaps eventually some form of caching kicks in...
I think it's due to an initial delay in loading modules or accessing RAM. If you switch the functions without looping the first one comes out way slower no matter what is tested. In any case the time difference is so negligible that it really doesn't matter. We're talking a few ten thousandths of a second on 10,000 loops.
Benchmarking this "piece of code" is absolutely irrelevant.. Either is fine..and it makes no difference which "method" you use. It's what you do with the data which will reflect speed and memory.
I agree with that it doesn't realy matter but for coding standards you should use $var . $var2 instead of "$var$var2"
I agree with jestep. "$thisvariable" gets evaluated in a way that's usually a little slower than $thisvariable does.