Board index » delphi » Adding Strings to Tstrings-fast

Adding Strings to Tstrings-fast

Does anyone know how to get at the memory used by the tstrings object?

I'm after appending one stringlist to another, but typically both lists
contain a large amount of data.
Using List.AddStrings is very slow, so I'd like, if possible to find out how
to pick up the memory address of the first item in the second list and point
the last 'next pointer' in the first list to it, thereby hopefully
concatenating the second list to the first.
Is this possible, and if so, how is it done?  I'd prefer not to spend ages
looking thorugh microsoft classes unit, or whatever it is.....

Thanks,

Martin Platt.

 

Re:Adding Strings to Tstrings-fast


In article <72euc6$6m...@news1.cableinet.co.uk>, "Martin Platt"

Quote
<martin.platt.nos...@kewill-xetal.com> writes:
>I'm after appending one stringlist to another, but typically both lists
>contain a large amount of data.
>Using List.AddStrings is very slow, so I'd like, if possible to find out how
>to pick up the memory address of the first item in the second list and point
>the last 'next pointer' in the first list to it, thereby hopefully
>concatenating the second list to the first.
>Is this possible, and if so, how is it done?  I'd prefer not to spend ages
>looking thorugh microsoft classes unit, or whatever it is.....

How many are you adding, are you displaying them at the same time, are they
sorted.

All these can have some effect, I've not found adding strings terribly slow.

If you are displaying them, use BeginUpdate / EndUpdate.

If they are sorted, add them unsorted and then set Sort to true.

You could concatenate the strings and then allocate them to the Text property
of the  TStringList.

Alan Lloyd
alangll...@aol.com

Re:Adding Strings to Tstrings-fast


On 12 Nov 1998 20:50:45 GMT, alangll...@aol.com (AlanGLLoyd) wrote:

Quote

>In article <72euc6$6m...@news1.cableinet.co.uk>, "Martin Platt"
><martin.platt.nos...@kewill-xetal.com> writes:

>>I'm after appending one stringlist to another, but typically both lists
>>contain a large amount of data.
>>Using List.AddStrings is very slow, so I'd like, if possible to find out how
>>to pick up the memory address of the first item in the second list and point
>>the last 'next pointer' in the first list to it, thereby hopefully
>>concatenating the second list to the first.
>>Is this possible, and if so, how is it done?  I'd prefer not to spend ages
>>looking thorugh microsoft classes unit, or whatever it is.....

>How many are you adding, are you displaying them at the same time, are they
>sorted.

>All these can have some effect, I've not found adding strings terribly slow.

>If you are displaying them, use BeginUpdate / EndUpdate.

>If they are sorted, add them unsorted and then set Sort to true.

>You could concatenate the strings and then allocate them to the Text property
>of the  TStringList.

>Alan Lloyd
>alangll...@aol.com

In Delphi, if you have two TStringList s can you do something like:

  SList1 := SList1 + SList2;   (or something similar?)

I've done stuff like this with arrays and pointers in TP.  Just
wondering... (have not tried it yet).

  michael

Re:Adding Strings to Tstrings-fast


It could be 1000 strings.
The slow bit is that I have 7 lists of the same amount, and two sets of
these seven lists.
At times during the program it is desirable to add the second stringlists to
the first lot.  The reason is that I'm then assigning the Cols property of a
string grid with these lists, and would like to do the thing in one go,
instead of adding singly, or using 'AddStrings', because they take some time
to complete.

Cheers.

Martin.

Re:Adding Strings to Tstrings-fast


In article <72gq3v$n6...@news1.cableinet.co.uk>, "Martin Platt"

Quote
<martin.platt.nos...@kewill-xetal.com> writes:
>It could be 1000 strings.
>The slow bit is that I have 7 lists of the same amount, and two sets of
>these seven lists.
>At times during the program it is desirable to add the second stringlists to
>the first lot.  The reason is that I'm then assigning the Cols property of a
>string grid with these lists, and would like to do the thing in one go,
>instead of adding singly, or using 'AddStrings', because they take some time
>to complete.

What you could do is to add enough strings to each col to fill the displayed
rows of the string grid, then add the others later - it appears much faster to
the user.

As :-

var
  MyStringList : TStringList;
  i, j : integer;
begin
  StringGrid1.RowCount := 1000;
  MyStringList := TStringList.Create;
  with MyStringList do begin
    {add 10 rows to each column}
    for j := 0 to 6 do begin
      Clear;
      for i := 1 to 10 do  // add the top visible rows
        Add(Format('Col %d String %2.2d', [j, i]));
      StringGrid1.Cols[j] := MyStringList;
    end; {for j := 0 to 6}
    StringGrid1.Refresh;
    {add rows 11 to 1000 for each column}
    for j := 0 to 6 do begin
      Clear;
      AddStrings(StringGrid1.Cols[j]); // get the rows which have been filled
into the stringlist
      for i := 11 to 1000 do  // now add the rest of the rows.
        Add(Format('Col %d String %2.2d', [j, i]));
      StringGrid1.Cols[j] := MyStringList;
    end; {for j := 0 to 6}
    Free;
  end; {with MyStringList do}
end;

This gave me a time of 50ms for the display of the part-filled stringgrid and a
further 330mS for the display of the fully filled stringgrid. But of course the
user saw it apparently filled in 50mS.

This was with a 100MHz Pentium with 32Mb using Delphi 2.  But you may be doing
a lot more to calculate the cell contents.

Alan Lloyd
alangll...@aol.com

Re:Adding Strings to Tstrings-fast


Quote
: micha...@frontiernet.net (michael) wrote:

>In Delphi, if you have two TStringList s can you do something like:

>  SList1 := SList1 + SList2;   (or something similar?)

No, you cannot.

Check CLASSES.PAS on how to get at the internal list object - it's
possible at least for TLists.

--
Stefan Hoffmeister    (http://www.econos.de/)
No private email, please, unless expressly invited.

Re:Adding Strings to Tstrings-fast


On Thu, 12 Nov 1998 15:18:43 -0000, "Martin Platt"

Quote
<martin.platt.nos...@kewill-xetal.com> wrote:
>Does anyone know how to get at the memory used by the tstrings object?

You might want to look at the TStrings.GetText and TStrings.SetText
methods. The former will read the strings from a string list object into a
PChar buffer. If you do this with two PChar buffers, one for each string
list object, you should be able to use one of the string-handling routines
(like StrCat) to concatenate the two values into one PChar buffer. Then use
the SetText method to copy the concatenated data into a single string list
object.

//////////////////////////////////////////////////////////////////////////
Steve Koterski                      "The knowledge of the world is only to
Technical Publications              be acquired in the world, and not in a
INPRISE Corporation                 closet."
http://www.inprise.com/delphi          -- Earl of Chesterfield (1694-1773)

Re:Adding Strings to Tstrings-fast


Quote
skoter...@NOSPAMinprise.com (Steve Koterski) writes:
>On Thu, 12 Nov 1998 15:18:43 -0000, "Martin Platt"
><martin.platt.nos...@kewill-xetal.com> wrote:

>>Does anyone know how to get at the memory used by the tstrings object?

>You might want to look at the TStrings.GetText and TStrings.SetText
>methods. The former will read the strings from a string list object into a
>PChar buffer. If you do this with two PChar buffers, one for each string
>list object, you should be able to use one of the string-handling routines
>(like StrCat) to concatenate the two values into one PChar buffer. Then use
>the SetText method to copy the concatenated data into a single string list
>object.

>//////////////////////////////////////////////////////////////////////////
>Steve Koterski                      "The knowledge of the world is only to
>Technical Publications              be acquired in the world, and not in a
>INPRISE Corporation                 closet."
>http://www.inprise.com/delphi          -- Earl of Chesterfield (1694-1773)

A look at D3's source reveals that SetText does not do its job at a low level,
i.e. it will break up the PChar in a loop and then uses Add().

Looks like if you want fast processing, you have to write your own routines.

Thomas Nagy
-----------------------------------------------------------
Check out my cats! (http://members.tor.shaw.wave.ca/~tnagy/)

Other Threads