Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Best way to use TList?

0 views
Skip to first unread message

Jukka Palomäki

unread,
Oct 31, 1996, 3:00:00 AM10/31/96
to

Function ListIndex1 makes exe-file smaller than ListIndex2 even if
ListIndex1 has a local variable. Why?
Which of them is faster?

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

Martin Larsson

unread,
Oct 31, 1996, 3:00:00 AM10/31/96
to

Jukka Palomäki wrote:
>
> Function ListIndex1 makes exe-file smaller than ListIndex2 even if
> ListIndex1 has a local variable. Why?

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

Martin Larsson

unread,
Nov 4, 1996, 3:00:00 AM11/4/96
to

Jukka Palomäki wrote:

>
> Martin Larsson wrote:
> >
> > Jukka Palomäki wrote:
> > >
> > > Function ListIndex1 makes exe-file smaller than ListIndex2 even if
> > > ListIndex1 has a local variable. Why?
> >
> > 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).
>
> That was just an example. In real situation I use list of lists of lists
> of lists plus some other lists. The program is getting bigger all the
> time (now about 11 000 lines) and that's why I would like to know what is
> the most effective way to use lists.

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.

0 new messages