A question on performance (article related)
Posted: Sun Mar 19, 2006 10:21 am
I accidentally found this article: http://www.devarticles.com/c/a/PHP/Buil ... ith-PHP/1/
I read it and now I've got a question regarding the inefficiency it describes. Here are the two code snippets. The first is inefficient and slow (according to the article), and the second one is the correct one.
Actually I don't see the horrible inefficiency the article describes. Do you?
I read it and now I've got a question regarding the inefficiency it describes. Here are the two code snippets. The first is inefficient and slow (according to the article), and the second one is the correct one.
Code: Select all
function getEmployees()
{
$query = "SELECT id FROM persons WHERE companyid = $this->companyid";
$stmt = execute_query($query);
$this->employees = array();
while ($row = fetch_object($stmt)
{
$this->employess[$row->id] = new Person($row->id);
}
return $this->employees;
}
$company = new Company("Datagate");
$employees = $company->getEmployees();
foreach ($employees as $id =>$employee)
{
$employee->addVacationDays(3);
}Code: Select all
function getEmployees()
{
$query = "SELECT id FROM persons WHERE companyid = $this->companyid";
$stmt = execute_query($query);
$this->employees = array();
while ($row = fetch_object($stmt)
{
$this->employess[$row->id] = $row->id;
}
return $this->employees;
}
$company = new Company("Datagate");
$employees = $company->getEmployees();
foreach ($employees as $id)
{
$employee = new Employee($id);
$employee->addVacationDays(3);
}