开发者

PHP: Which is faster - array_sum or a foreach?

I'm trying to work out which method would be faster (if either would be?). I've designed a test here: http://codepad.org/odyUN0xg

When I run that test my results are very inconsistent. Both vary wildly.

Is there a problem with the test?

otherwise, can anyone suggest which one would or wouldn't be faster??

Edit

Okay guys, thanks I've edited the codepad here: http://codepad.org/n1Xrt98J With all the comments and discussion i've decided to go with array sum. As soon as I used that microtime(true) thing it started to look alot faster 开发者_JAVA技巧(array_sum)

Cheers for the advice, oh and I've added a "for" loop so that results are more even, but as noted on the results there is little time saving if any over a foreach.


The problem is that you took a very low limit, 1000. The overhead of the PHP interpreter is MUCH larger. I would take 100000000 or something like that.

However I think array_sum is faster since it's more specialized and probably implemented in fast C.

Oh, and as Michael McTiernan said you must change every instance of microtime() to microtime(true). http://php.net/manual/en/function.microtime.php

And finally, I wouldn't use codepad as testing environment since you have no control over it. You have no idea what happens and whether your process is paused or not.


To be honest, there's little value in using an artificial test and either way this sounds like fairly pointless micro-optimisation unless you've specifically identified this as a problem area after profiling the necessary code.

As such, it probably makes sense to use which ever feels more appropriate. I'd personally plump for array_sum - that's what it's there for after all.


Change any instance of microtime() to microtime(true).

Also, after testing this, the results aren't that wildly different.


$s = microtime();

A call to microtime() with no arguments will return a string like this: 0.35250000 1300737802. You probably want this:

$s = microtime(TRUE);

array sum took 0.125188 seconds sum numbers took 0.166603 seconds

These kind of tests need to be run a few thousand times so you can get large execution times that are not affected by tiny external factors.


You need much bigger runs, and need to average several of them. You should also separate the two tests into two files. The server has other things going on that will disturb test timing, hence the need to average many runs.

array_sum() should be the faster of two as there is no extra script parsing associated with it, but its worth checking.


Almost always array_sum is faster. It depends more on your server php.ini configuration than actual usage between array_sum and foreach.


Coming to the point of this question, for a sample setup like following:

<?php 

set_time_limit(0);

$s = microtime(TRUE);

$array = range(1, 10000);
$sum = 0;
for($j = 0; $j < 1000; $j++){
    $sum += array_sum($array);
}
$s1 = microtime(TRUE);
$diff = $s1 - $s;
echo "for 1000 pass, array_sum took {$diff} seconds. Result = {$sum}<br/>";




$sum = 0;
$s2 = microtime(TRUE);
for($j = 0; $j < 1000; $j++){
    foreach($array as $val){
        $sum += $val;
    }
}
$s3 = microtime(TRUE);
$diff = $s3 - $s2;
echo "for 1000 pass, foreach took {$diff} seconds. Result = {$sum}<br/>";

I got results where foreach was always slower. So that should answer your question. Sample:

for 1000 pass, array_sum took 0.2720000743866 seconds. Result = 50005000000
for 1000 pass, foreach took 1.7239999771118 seconds. Result = 50005000000
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜