IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

July Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...Using Data   Print This    All Groups  
  From the October 2015 Issue of Prestwood eMag
 
Delphi Using Data:
TStringList - Maximizing Performance
 
Posted 12 years ago on 6/11/2008
Take Away: Before adding many new entries to a TStringList, set its Sorted property to false. Add all your entries. Finally, set the Sorted property back to True (if desired). Sorting is expensive, in terms of macnine cycles. By leaving the Sorted property set to True, you force the TStingList to re-sort itself after each entry. This can significantly slow things down. This applies to TStrings, as well, and all components that have a TStrings property, like TListBox, etc.
 A flashcard from our Delphi Flashcards Library
 A tip from our Delphi Tips Page

KB101180

Before adding many new entries to a TStringList, set its Sorted property to false.  Add all your entries.  Finally, set the Sorted property back to True (if desired)

Sorting is expensive, in terms of macnine cycles.  By leaving the Sorted property set to True, you force the TStingList to re-sort itself after each entry.  This can significantly slow things down.  Your results will be much faster if you add while Sorted is false, then let the TStringList do the sorting all at once, when you're finished.

This applies to TStrings, as well, and all components that have a TStrings property, like TListBox, etc.

Before adding many new entries to a TStringList, set its Sorted property to false.  Add all your entries.  Finally, set the Sorted property back to True (if desired)


Comments

1 Comments.
Share a thought or comment...
Comment 1 of 1

The "correct" way is to use BeginUpdate / EndUpdate regardless of the Sorted flag, even when adding multiple lines to a memo, eg.

oStrings.BeginUpdate;
try
  for nLoop:=0 to 10000 do
    oStrings.Add(IntToStr(nLoop));
finally
  oStrings.EndUpdate;
end;
---
AzzaAzza69
Posted 51 months ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P125A1
Enter key:
Tip Contributed By Wes Peterson:

Wes Peterson is a Senior Programmer Analyst with Prestwood IT Solutions where he develops custom Windows software and custom websites using .Net and Delphi. When Wes is not coding for clients, he participates in this online community. Prior to his 10-year love-affair with Delphi, he worked with several other tools and databases. Currently he specializes in VS.Net using C# and VB.Net. To Wes, the .NET revolution is as exciting as the birth of Delphi.

Visit Profile

 KB Article #101180 Counter
20229
Since 6/11/2008
Sales Website: www.prestwood.com Or visit our legacy sales site: 
legacy.prestwood.com


©1995-2020 Prestwood IT Solutions.   [Security & Privacy]