Re: which one is faster

From: Date: Fri, 08 Oct 2010 16:16:56 +0000
Subject: Re: which one is faster
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-308577@lists.php.net to get a copy of this message
chris h wrote:
Saeed here's a quick (and dirty) test I ran: $tests = 1000000; $start = microtime(true); for ($i=0; $i<$tests; $i++) { $a = md5( rand() ); $b = md5( rand() ); $c = $a.$b; } var_dump( "By concat op:\t". (microtime(true) - $start) );
that's not a fair test because you have rand() and md5() calls in there (something temporally varying) Here's a quick test script which does 100 million iterations on both, 3 times to get some half measurable results $i = $its = 100000000; $tests = 3; $a = 'foo'; $b = 'bar'; while($tests-->0) { $t = microtime(true); while($i-->0) {
    $c = "$a$b";
} echo 'time .: ' . (microtime(true)-$t) . PHP_EOL; $i = $its; $t = microtime(true); while($i-->0) {
    $c = $a.$b;
} echo 'time ": ' . (microtime(true)-$t) . PHP_EOL; } I also ran the tests in the opposite order just to ensure they were fair, results are that $a.$b (concatenation) averaged 22 seconds, and the "$a$b" approach was 28 seconds. Thus, concatenation is faster - but you have to get up to circa 10 million+ uses per second to use it. Best, Nathan

« previous php.general (#308577) next »