|
Posted by Markus Ernst on 11/09/06 13:54
Jerry Stuckle schrieb:
> Markus Ernst wrote:
>> Hello
>>
>> A class that composes the output of shop-related data gets some info
>> from the main shop class. Now I wonder whether it is faster to store
>> the info in the output class or get it from the main class whenever it
>> is needed:
>>
>> class shop_main {
>> var $prices = null;
>> function &get_prices() {
>> if (is_null($this->prices)) {
>> $this->prices =& $this->db->getCol('SELECT id FROM shop_prices');
>> if (!is_array($this->prices)) $this->prices = array();
>> }
>> return $this->prices;
>> }
>> }
>>
>> // case 1: store info in class
>> class shop_output {
>> var $prices = null;
>> var $main_shop_obj;
>> function do_something($product_id) {
>> if (is_null($this->prices)) {
>> // assume that main shop object is already set
>> $this->prices =& $this->main_shop_obj->get_prices();
>> }
>> foreach ($this->prices as $price_id) {
>> // do something...
>> }
>> }
>> }
>>
>> // case 2: get info each time it is needed
>> class shop_output {
>> var $main_shop_obj;
>> function do_something($product_id) {
>> // assume that main shop object is already set
>> foreach ($this->main_shop_obj->get_prices() as $price_id) {
>> // do something...
>> }
>> }
>> }
>>
>> BTW I also wonder whether it is appropriate to pass the prices by
>> reference here - I assume it is, but in an older thread on performance
>> Chung Leung stated that passing by reference can decrease performance
>> in some cases.
>>
>> Thanks for a comment!
>> Markus
>
> Impossible to tell from your comments. There are way too many variables.
>
> For instance - if your main shop class has 10 values and you need each
> of them 50 times, and those values don't change, it's probably more
> efficient to cache them in the output class.
>
> OTOH, if you have 1M items in the main class which change frequently and
> you only need 2 items from it, it's more efficient to get them from the
> main class each time.
Thank you, this helps a lot as a guideline.
> And Chung is correct. Many times passing by reference is faster, but
> occasionally it's slower. However, that should not be a reason you
> decide to pass by reference or not. Or if it is, it should be about
> number 247 on your list.
>
> The real question is - are you actually having performance problems, or
> are you spinning your wheels on something which may or may not be a
> problem? I suspect the latter.
Your suspection is correct. After I _had_ performance problems (30+
seconds execution time for displaying 20 or 30 items...) I started
caring for performance issues at all, and now I try to be aware of them
even before problems occur. With my poor technical background (I don't
have a basic IT education) this sometimes makes me spend too much effort
on questions of low priority. Thus, comments of the kind you posted are
very helpful for me, thanks!
Navigation:
[Reply to this message]
|