function TMyObject.ListIndex1(key: String): integer;
var listObject: TListObject;
begin
for Result := 0 to myList.Count - 1 do
begin
listObject := TListObject(myList[Result]);
if listObject.key = key then
Exit;
end;
end;
function TMyObject.ListIndex2(key: String): integer;
begin
for Result := 0 to myList.Count - 1 do
if TListObject(myList[Result]).key = key then
Exit;
end;
Thank you
Jukka Palomäki
Don't know.
> Which of them is faster?
Test them, and you'll probably find the difference is so small it
doesn't matter which one you use (if they end up different).
But:
The variable used in a for loop does not have a value after the for
loop, so if your search fails, you can get anything returned from
the functions.
M.
--
Ettertraktet kaffe, er det ekstra god kaffe?
mailto:martin....@delfi-data.msmail.telemax.no
http://www.delfidata.no/users/~martin
Ah. I believe, if you turn on optimization, they will end up equal.
Using nested TLists is probably not the fastest way to go around
your problem, though. It seems to me you’re building a database of
sorts in memory, so hash-tables, quad-trees and the like might get
you much more speed increase than just accessing TLists the fastest
way.
TLists aren’t too bad, being arrays, but the extra access through
the Items property might not be the most efficient. It includes a
function call and bounds-checking. Try using the List property
instead, it’s the actual array of the TList.
And, of course, the lists should be sorted if speed is of essence,
since a binary search of a sorted array is quite fast. But sorting
that array... That depends highly on the usage of the array. If create
them once, and then just use them (no adding, except in the beginning),
array's are cool. But if you're adding and deleting all the time,
a B-tree might be better. Or a hash-table.
So, the answer to your problem is:
Don't worry about the speed of the TList too much, instead, think about
what algorithm is the best for your application. TList might fit that
bill. Then let the optimizer handle the speed issue. It will probably
be a lot better than you at getting top speed.