views:

343

answers:

2

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?

+3  A: 

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 :)

Gordon
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.
Louis
Well, switch to regular arrays then and use a for loop. Without seeing any code, this is the best idea I can offer.
Gordon
+1  A: 

Best solution was to use a for loop and a Singleton class with the indexes and iteration methods.

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