Re: class object vs array for db table model

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Oct 12, 2010 at 4:45 AM, chris h <chris404@xxxxxxxxx> wrote:
>

<snip>

>
>
> When you are adding a row as an object you are calling 4 user
> functions:ÂMyTable::__construct(),ÂMyTable::setId(),ÂMyTable::setName(), and
> MyTable::setDescription(). ÂThis adds some overhead for sure, so you might
> want to think about passing the row array into the construct and doing away
> with the setters (at least for the initial instantiations).
> Something like this...
> -----
> public Âfunction __construct($dataArray=null)
> {
> ÂÂforeach ((array)$dataArray as $rowKey => $rowValue)
> ÂÂ{
> ÂÂ Â$this->$rowKey = $rowValue;
> ÂÂ}
> }
> ==OR==
> public Âfunction __construct($dataArray=null)
> {
> ÂÂ// maybe add casting here as well?
> ÂÂ$this->_id       Â= isset($dataArray['id'])?
> ÂÂ$dataArray['id']: null;
> ÂÂ$this->_nameÂÂ Â Â Â = isset($dataArray['name'])?
> $dataArray['name']: null;
> ÂÂ$this->_descriptionÂÂ= isset($dataArray['description'])?
> Â$dataArray['description']: null;
> }
> ==And instantiate like this==
> $list = array();
> while ($row = $db->fetch($result))
> {
> ÂÂ// I also changed setting the $list's key with "$row['id']" instead of
> using the MyTable getter.
> ÂÂ$list[$row['id']] =Ânew MyTable($row);
> }
> ---
> I don't know how much faster that will run for you (if at all) but using
> your method you were calling 5 user functions per result set from the db
> (that's the construct, the getter to set the $list key, and 1 for each of
> the 3 properties of the result) for a total ofÂ18,420 user function calls.
> ÂWith my method I'm calling 1 user function (the construct) for each set,
> for a total ofÂ3,684 calls. ÂOf course a down-side is that if there's any
> logic in your setters then that needs to be replicated in your construct.
> Another method is to use a single object for allÂ3684 records. ÂPerhaps you
> can use the built in Iterator interface which lets your class's objects be
> used as if they were an array for the purposes of foreach loops.
> http://php.net/manual/en/language.oop5.iterations.php
> You would set a pointer to the record you wish to use, and your getters and
> setters would key off thatÂelementÂof the master array. ÂThis should be a
> fast solution, while still giving you the power ofÂencapsulation, getters
> and setters.
>
> Hope that helps!
> Chris.
>

Hi Chris,

Thanks for the reply.  The sample I made is just for simplicity and
comparison.  As for function calling 'get's, I think the speed would
come out to be same in your sample since you're doing isset()
checking.  My actual class is more sophisticated having the psuedo
overloading as you mentioned.  The class is generated from a PHP class
builder - to save on typing - I made a long time ago with some minor
update.

Anyway, I was frustrated as to why my code took so long to execute and
had to dig deep.  As it turns out, I had xdebug loaded with all
options on ... lol.  Removed the extension and all is good in the
world.  The script runs in less than 150ms :D!!!

Thanks,
Tommy

PS:  This is what I get for not coding in PHP so long ...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux