Re: String manipulation, optimize



Ach, the meaningless debate that won't go away! The difference is so
miniscule that no one really knows which one is faster in a real-life
situations. Benchmark codes just give you bogus answers. The following
snippet, for instance, shows that the concatenation is more than an
order of magnitude slower than interpolation:

function getmicrotime() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}

$i = "hello";
$a = array_fill(0, 30000, "\$i");
$code1 = "\$s = " . implode(".", $a) . ";";
$code2 = "\$s = \"" . implode("", $a) . "\";";

$time_start = getmicrotime();
eval($code1);
$time_end = getmicrotime();
$time1 = $time_end - $time_start;

$time_start = getmicrotime();
eval($code2);
$time_end = getmicrotime();
$time2 = $time_end - $time_start;

echo "$time1 vs $time2";


The bottomline: it doesn't matter.

.