Page 1 of 1
Finding how far though an itererator you are
Posted: Fri Aug 07, 2009 11:42 am
by ConvertFromOldNGs
by Torrie Moore >> Fri, 25 Jun 1999 5:09:06 GMT
Does anyone know if there is a fast way of determining the position of an iterator in its collection.
I have managed to do this by the following code
iter.StartAtObject(obj);
index := iter.getCollection.indexOf(obj);
The second line of code takes about 10 seconds for a dictionary containing 60000 records. Does anyone know of a faster way of getting this information?
Re: Finding how far though an itererator you are
Posted: Fri Aug 07, 2009 11:42 am
by ConvertFromOldNGs
by Craig Shearer >> Sun, 27 Jun 1999 20:00:21 GMT
That's because it seems to be doing a sequential search to find the object. I bet if you look to an object that happens to be at the start of the collection, it will be quick.
One way to speed this up (if the collection is a dictionary) would be to perform a binary search.
I did something similar, as an experiment a while ago to update the position of the thumb on a scrollbar on a virtualised table control. You can do a binary search with a limited number of iterations, say 5, and get a remarkably accurate result of approximately where the object lies in the collection.
Craig.
Re: Finding how far though an itererator you are
Posted: Fri Aug 07, 2009 11:42 am
by ConvertFromOldNGs
by Arjan van Hasselt >> Wed, 30 Jun 1999 2:03:57 GMT
I use the following code to position a scrollbar slider next to a listbox. With a collection of 50,000 objects it takes only
0.017 seconds to execute the entire code segment regardless of where you are in the collection. =========================================================================== vars
count : Integer;
st : Time;begin
app.mousePointer:= self.MousePointer_HourGlass;
st := app.clock.Time;
listBoxRight.clear;
app.myCompany.allProducts.startKeyGeq(textBoxRightStart.text, myIterator);
while count < listBoxRight.lines and myIterator.next (myProduct) do
count := count + 1;
listBoxRight.addItem (myProduct.name);
endwhile;
epilog
listBoxScrollBar.value := app.myCompany.allProducts.indexOf(myProduct); if myProduct = null then
listBoxScrollBar.value := listBoxScrollBar.max;
endif;
labelRight.caption :="Time Taken := " & ((app.clock.Time - st).Integer/1000).String & " Seconds";
app.mousePointer:= self.MousePointer_Arrow;
end; ============================================================================ =
My machine is a Pentium II 266 with 64 MB of RAM running single user mode.
I am sure this would be slightly longer when network traffic is involved.
Cheers ... AJ
Re: Finding how far though an itererator you are
Posted: Fri Aug 07, 2009 11:42 am
by ConvertFromOldNGs
by Craig Shearer >> Wed, 30 Jun 1999 3:52:24 GMT
Interesting, the benefits of single user!
I just did an experiment running multi-user with a collection of around 1800 objects. Here's the code:
vars
e: Employer;
f, l : Employee;
t: Integer;
begin
e := app.root.getEmployers.getAtKey("90B", "B9999");
f := e.getEmployeesById.first;
l := e.getEmployeesById.last;
t := app.clock;
write e.getEmployeesById.indexOf(f);
write (app.clock - t);
t := app.clock;
write e.getEmployeesById.indexOf(l);
write (app.clock - t);
end;
Getting the index of the first object takes 10 milliseconds. Getting the index of the last object takes 100 milliseconds. Oh, and I have a (very) lowly 166MHz Pentium.
Craig.