TList VS. TStringList lazarus / 21-General ---------------------------------------------------------------------- stoffman · 2021-01-21 16:06 Hi, I'm doing some string processing in my application, it doesn't have to interface with a UI and needs to be compatible with Delphi... So what would you prefer using for handling list of strings TStringList or TList ? and why? Did someone benched mark operation on them? memory consumption? Thanks. ---------------------------------------------------------------------- howardpc · 2021-01-21 16:23 You don't specify what "processing" exactly your strings need to undergo. However, don't overlook a simple dynamic array of strings. Such arrays are lightweight compared to most list classes, and their memory usage is managed for you by the compiler. How many strings are you dealing with? ---------------------------------------------------------------------- ASerge · 2021-01-21 16:31 Quote from: stoffman on January 21, 2021, 05:06:45 pmSo what would you prefer using for handling list of strings TStringList or TList ? and why? TList will take a little less RAM due to the absence of the Objects field, but much more in the executable file, because in the UI program, TStringList is definitely already used, but TList is not. In addition, if I'm not mistaken, so far FPC does not combine the specialized generics for the same type from different modules. Both use range checking and data modification events. In my opinion, generics usually complicates the program. I recommend TStringList. ---------------------------------------------------------------------- stoffman · 2021-01-21 17:54 @Aserge Amazing. Every unit that makes use of TList adds 10k for the executable! wow. That really caught me by surprise. @howardpc I really don't like the the ergonomics of dynamic arrays... ---------------------------------------------------------------------- JuhaManninen · 2021-01-21 19:35 I agree that TStringList makes more sense than TList. Generics are useful for types which do not have built-in container- etc. types. Quote from: stoffman on January 21, 2021, 06:54:52 pmAmazing. Every unit that makes use of TList adds 10k for the executable! wow. That really caught me by surprise. If you define a new type only in one place, like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TMyStringList = specialize TList;and use it in other units, I think it will add 10k total. Anyway, if you only add strings to a list and later read then out, TStringList is good. Often however it is used in wrong places which leads to poor performance. If you want to search for strings fast, you sort the list. Then Find() and IndexOf() use binary search O(log2(n)). Good. In some use cases you must add more items to a list and then search again. Adding items to a sorted list is very slow. First it does a binary search and then moves a big chunk of existing items a step forward. Same problem when deleting items. Many Delphi programs do that because traditionally there were no good container libraries. Now there are choices. For lots of data a HashMap is superior. It uses more memory than some other containers and resizing is "expensive". A good compromise is a balanced tree. It does a fast binary search when adding and retrieving, but does not need to move data around. Unit AvgLvlTree in LazUtils has a nice optimized TStringMap, and others like TStringToPointerTree and similar. LazUtils also has unit LookupStringList with a hybrid unsorted StringList with a fast lookup feature. I don't know if anybody uses it. ---------------------------------------------------------------------- stoffman · 2021-01-22 12:27 @JuhaManninen Thanks for the detailed answer. In my case a simple list is really all I need.  Defining a new type really did the trick and the exe size was reduced. ---------------------------------------------------------------------- AlexTP · 2021-01-22 13:15 >O(log2(n)). Since the log2(x) is log10(x)*some_const, and for any X: logX is log10*some_const, we can write quoted text shorted: O(log(n)) ---------------------------------------------------------------------- JuhaManninen · 2021-01-22 15:41 Quote from: Alextp on January 22, 2021, 02:15:36 pmSince the log2(x) is log10(x)*some_const, and for any X: logX is log10*some_const, we can write quoted text shorted: O(log(n)) Yes, I guess that is the right Big-O notation. I don't remember details. Big-O also includes only the highest degree exponent term and leaves out any constant coefficients. It is all about scalability.