...

View Full Version : is sprintf or implode faster?



Apothem
07-15-2008, 08:58 PM
function prepare() {
return implode('', func_get_args());
}

So, if I use this instead of sprintf, will it be faster?
e.g will:

$t = "good";
$v = "stuff";
$var = prepare('This text is: ', $t , ' while that text is: ', $v);

be faster than:


$t = "good";
$v = "stuff";
$var = sprintf('This text is: %s while that text is: %s', $t, $v);

Fou-Lu
07-15-2008, 09:06 PM
Sprintf is faster by almost 280%. The function call is invalid as well as you can't use func_get_args() in another function call unless the function prototype has been declared as by reference.


function prepare()
{
$argv = func_get_args();
return implode('', $argv);
}

will work however.

You know if you use microtime you can do these tests yourself to see which is faster >.<

Apothem
07-15-2008, 09:10 PM
Yea... but everytime I do it, I get a number over 1000.


function microtime_start() {
global $start;
list($usec, $sec) = explode(' ', microtime());
$start = (float)$usec + (float)$sec;
}

function microtime_end() {
global $start;
list($usec, $sec) = explode(' ', microtime());
$end = (float)$usec + (float)$sec;
$end = (float)$end - (float)$start;
echo $end;
}

is what I used.

Fou-Lu
07-15-2008, 09:16 PM
microtime has been upgraded nicely, I don't even remember the old school way of doing it. Takes a boolean result to determine if it should return its value as a float:


$start = microtime(true);
sleep(1);
$end = microtime(true);
printf("Time spent: %f", ($end - $start));

Nice and easy :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum