Thursday, April 28, 2011

PHP Lazy Load Iterator

I have an iterator class that loops over an array of objects and lazily loads from the database when it needs to (when it's not loaded into memory). Problem is this is iterating around 200,000 times and I found out from here: http://www.garfieldtech.com/blog/magic-benchmarks that the iterator interface is incredibly slow.

Would anyone know of a good way to lazy load without using an iterator interface?

From stackoverflow
  • You could use a plain old for loop.

    The iterator interface might be slow, but when doing 200.000 DB queries, chances are pretty good your bottleneck is not the iterator. I'd suggest to profile your code to see if it is really in need of optimization at this position.

    Premature Optimizatizion is the root of all evil :)

    Louis : Actually it will be doing about 10 queries. Each row holds serialized data of about 100 objects. I've done much profiling and this looks like the problem unless there are issues with array access times.
    Gordon : Well, switch to regular arrays then and use a for loop. Without seeing any code, this is the best idea I can offer.
  • Best solution was to use a for loop and a Singleton class with the indexes and iteration methods.

    Gordon : so you did what I suggested but then decided to provide your own answer for acceptance? Too kind.

0 comments:

Post a Comment