Page 1 of 1

+= or array_sum()

Posted: Thu Dec 03, 2009 8:28 pm
by Weiry
I ran into an interesting issue where an assignment operator ( += ) will actually run slower than the sum of an array.
Maybe its my understanding of how the calculations work exactly, but shouldn't a += take the same amount of processing time as array_sum()? As essentially they do the exact same thing.

The database table consists of 6 entries:

Code: Select all

 
ID    |    aValue
---------------
1     |    1
2     |    2
3     |    4
4     |    3
1     |    5
1     |    3
 
Here are my current examples. Each test was conducted 10,000 times.

Code: Select all

$result     =   $database->query("SELECT `aValue` FROM `aTable` WHERE `ID` = '1' ORDER BY `aValue` DESC");
$numrows    =   $database->numRows($result);
while($row = $database->fetchArray($result)){
    $sum_total += $row['aValue'];
}
$new_aValue =   $sum_total/$numrows;
The above takes an average time taken: 0.00037233381271362 seconds.

Where as the following code,

Code: Select all

$query2      =   "SELECT * FROM aTable WHERE ID = '1' ORDER BY aValue DESC";
$result2     =   $database->query($query2);
$numrows2   =   $database->numRows($result2);
             
while($row2 = $database->fetchArray($result2)){
    $aValue2[] = $row2['aValue'];
}
$sum_total2  =   array_sum($aValue2);
             
$new_aValue2 =   $sum_total2/$numrows2;
Has an average time taken: 0.00019129695892334 seconds.

The first sample of code i see to be easier to read and shorter on the line count.
The second sample of code has to create a new array in order to process the same information and yet has a faster processing time.

Is there a reason why there is such a significant difference in time taken?
Could this also be compared through memory usage? ie. A local variable might take up more memory than an array?

Re: += or array_sum()

Posted: Thu Dec 03, 2009 8:45 pm
by Eran
Probably array_sum has some optimizations to quickly sum an array as opposed to doing it explicitly in PHP. But how about
[sql]SELECT AVG(`aValue`) AS `average` FROM `aTable` WHERE `ID` = 1[/sql]

Re: += or array_sum()

Posted: Thu Dec 03, 2009 8:55 pm
by Weiry
I understand that there it is possible in MySQL to calculate the average but what if the calculation was something other than the average? Such as an algorithm.
What im trying to understand is why the efficiency difference.

Re: += or array_sum()

Posted: Thu Dec 03, 2009 9:05 pm
by Eran
In my opinion you should not even bother with such micro-optimization unless you have a real reason. Use the method that is most maintainable and appropriate for you to use (be it PHP or MySQL). And as I've said, since array_sum is an internal function in compiled C, it probably has some optimizations that outperform PHP runtime. Possibly using a bytecode cache such as APC would reduce those differences.

Re: += or array_sum()

Posted: Thu Dec 03, 2009 9:17 pm
by Weiry
So essentially even though some options may in fact run quicker (slightly), the ease of maintaining the code and/or readability can in fact rank higher to maintain a higher quality of code over minor efficiency?

Makes sense, and thanks for shedding some light on the array_sum(), i might try to use more inbuilt functions rather than doing minor things manually.

Re: += or array_sum()

Posted: Thu Dec 03, 2009 9:30 pm
by John Cartwright
Weiry wrote:So essentially even though some options may in fact run quicker (slightly), the ease of maintaining the code and/or readability can in fact rank higher to maintain a higher quality of code over minor efficiency?
Absolutely. Premature optimization is the root of evil.

Re: += or array_sum()

Posted: Fri Dec 04, 2009 1:29 am
by Apollo
John Cartwright wrote:Absolutely. Premature optimization is the root of evil.
+1, fully agree!
Weiry wrote:Makes sense, and thanks for shedding some light on the array_sum(), i might try to use more inbuilt functions rather than doing minor things manually.
Yeah, certainly do so whenever you can. It's a smart thing to do, for more than one reason!