Martin_fr, OK those 2% is not exist, but this is still suprising that ShortString code have the same speed as code with AnsiString. I suspected that code with ShortStrings should be faster because it has better cache locality.
Well, in your example you are allocating the arrays, and then you only allocate strings. And your app has at that point not done anything to fragment the memory space.
So all the text data of all the strings is in one big block of continuous memory too. (whatever amount of that fit per cache line).
So all it needs is one extra cache line for the current fragment of the "array of internal pointers". That is just way below measurable.
Depending on the size and align requirements either type of string may have "bigger gaps". The shortstring will always carry the unused chars, as it allocates according to declaration, even if the data is shorter. but the longstring will (with most mem managers) allocate to a multiple of 16, and the allocation includes extra for the header.
Yet again, you will need extreme cases, and maybe even then not have enough to measure it.
Modern CPU do a lot of parallel stuff in their internal processing. That also leads to memory often being loaded to cache way ahead. After all once you loaded a cache line your code processes each byte (each char of the string) in that cache line (well not each, if the string has alignment padding at the end). But that gives likely a lot of time to load the next.
You could find out if you downclock your ram in your bios. You would have to process much larger amount of data though to see a difference. If you do, be sure to know how to get back. Wrong settings can make your system unstable.
(I did upgrade my RAM last year as I needed more. But the new one is also faster, and some (few) tasks improved by about 5% in speed. But only some few tasks).
Khrys, that's a lot of code just for setting string. Thank you for explanation.
As I pointed out in one of my earlier posts, when you do a lot of changes to a string, you may want to use pchar. But be sure to call uniquestring first.