0 Replies Latest reply on Nov 7, 2006 12:15 AM by obouillaud

    OOP : lazy vs load... why so slow ?

    obouillaud Level 1

      To be sure everybody understand (in my bad english, sorry) what I'm writing about, I explain the whole case :

      I made a "product" object which is a cfc with :
      - properties (a product is a book for example), so I have a title, summary, price ...
      - methods : set_title(), get_title(), ... and methods for loading/saving those information from/to the database. load() and save()

      I have a web page that displays for example 10 products... to stay in an object oriented concept, I made another object that creates the query from url parameters (order, restrictions on price...) and execute it.

      I made 2 solutions to populate my products objects before displaying them :
      1/ the first query has in it select par all the field needed to display the information of the product (title, price ...). Then I use what is call "lazy loading" which is to loop over the result of the query and use set_tile(query.title), set_price(query.price) ...
      2/the first query only contains the id of the products... then I loop over the result and call the load() method for each product (which makes 10 database calls if I want to display 10 products on my page).

      Lazy loading is you can imagine much more efficient... but also introduce more coupling between the objects (the cfc that creates the query needs to know which fields are required). On the other side the load() solution is database intensive...

      So I made a test... and guess what ! The "lazy" solution is not quicker than the "load" one !!!
      Both are slow (between 500 and 1500 ms) for the whole process to complete but there is no way to tell which solution is the fastest. It seems crazy for me...
      I guess that the reason why the lazy solution is slow is because I have many set_xxx() methods for each objects (let's says about 30). My set_xxx() are also a bit special (in fact I have a geneal set('name','value') method that checks if there is a function called set_name() in my object. If yes it passes the value to this method, if not, it just makes a cfset name = value... Yes I'm a lazy programmer and I don't want to create many functions like set_title(value) { cfset title = value } ... which is in my humble opinion a waste of time :-)

      But I can't understand that 300 calls to simple functions can takes 500 ms !