PHP | Updating array items through function while iterating - Big performance issue -


i iterating array more 3000 items in array looks that:

[   [     'id' => 1,     'type' => 'income'     'amount' => 10   ],    [     'id' => 2,     'type' => 'expense',     'amount' => 20   ],   .......   ] 

while iterating call functions manipulate array in same class that:

$this->data->revenue->each(function($row) use($user) {     if ($row->isincome())     {         $this->addrevenuerowtocolumn($row, 'income');          $this->addrevenuerowtocolumn($row, 'total');      }      if ($row->isexpense())     {         $this->addrevenuerowtocolumn($row, 'expense');          $this->subtractrevenuerowtocolumn($row, 'total');      } } 

this functions do:

protected function addrevenuerowtocolumn(&$row, $columnname) {     $this->report['bymonth'][$row->getmonthtablekey()]['bydepartment'][$row->department_id][$columnname] += $row->amount;     $this->report['bymonth'][$row->getmonthtablekey()]['total'][$columnname] += $row->amount;     $this->report['totals']['bydepartment'][$row->department_id][$columnname] += $row->amount;     $this->report['totals']['total'][$columnname] += $row->amount; }  protected function subtractrevenuerowtocolumn(&$row, $columnname) {     $this->report['bymonth'][$row->getmonthtablekey()]['bydepartment'][$row->department_id][$columnname] -= $row->amount;     $this->report['bymonth'][$row->getmonthtablekey()]['total'][$columnname] -= $row->amount;     $this->report['totals']['bydepartment'][$row->department_id][$columnname] -= $row->amount;     $this->report['totals']['total'][$columnname] -= $row->amount; } 

it takes 11 seconds process data , display it

what should do? in advance!

speed php solution
apart suggested use of database organize stuff, if still want hard way ;) can avoid iterating (let php internaly) on whole array using 1 of php functions below:

array_map — applies callback elements of given arrays

and

array_walk — apply user function every member of array

i see in code, have 'use' clause, presume php > 5.3. in case, can following:

$yourdata = array_map(                     function($row) use ($user)                     {                      /*$user->dostuff();*/                      return $row;                      }, $yourdata                     ); 

furthermore, lot of overhead display rendering part. if have lot of things display, example using simple echo, it's faster do:

$result = ""; $result .= $something; $result .= $somethingelse; echo $result; 

than

echo $something; echo $somethingelse; 

enhance solution use database
besides that, surely beneficial if use database. obvious thing store data in db tables , use sql-ish solution query it. speed script sure.

speed db+php solution
next, can major performance boost doing of calculations (business logic) inside database engine in form of stored procedures.

for example if you'd go mysql, create cursor , iterate on table rows in loop. on every row stuff, while ofcourse having direct access tables/columns of scheme (and possibly other stored procedures/functions). if doing lot of math-ish calculations it's great solution, ofcourse imho it's less convenient (more sophisticated) write stuff in sql rather php ;)


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -