arrays - Slow performance with cumulative count in PHP -
i have performance issues in chartcontroller. have 2 arrays: 1 array dates , 1 array entities contain date. want cumulative count date values of entities.
i have bit of code it's slow (i have wait 3 - 4 seconds 68k rows in data array).
my code:
// loop through sql results , count on date foreach ($result $row) { foreach ($dates $date => $value) { // $row['appointmentdate'] datetime object if ($date >= $row['appointmentdate']->format('ymd')) { $dates[$date]++; } } }
is there smarter / better / faster way achieve this?
// edit
jack gived me push needed , solved performance issue.
the code above took 38000 ms complete, code beneath 5700ms.
usort($result, function($a, $b) { return $a['appointmentdate'] > $b['appointmentdate']; }); // loop through sql results , count on date ($i = 0; $i++; $i <= count($result)) { $i++; $dates[$result[$i]['appointmentdate']->format('ymd')] = $i; }
thanks jack!
yes. can achieve better performance sorting 2 arrays before doing comparison. 2 arrays ordered can avoid doing n*m loop, , stay on n+m complexity (plus n*log n , m*log m sorting, anyway less n*m)
Comments
Post a Comment