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
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;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;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?