+= or array_sum()

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

+= or array_sum()

Post 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?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: += or array_sum()

Post 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]
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: += or array_sum()

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: += or array_sum()

Post 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.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: += or array_sum()

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: += or array_sum()

Post 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.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: += or array_sum()

Post 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!
Post Reply