[fpc-pascal] Speed question for strings
Vincent Snijders
vsnijders at quicknet.nl
Sat Nov 19 09:47:09 CET 2005
L505 wrote:
>>L wrote:
>>
>>>Why is the first and third example *so* much faster than the second example?
>>>Significantly faster.
>>>
>>
>>Because you doubled the number of string concatenations.
>>
>>Vincent.
>
>
>
> Right, but it's not twice as slow :-) Worse... It's at least 50-100 times slower.
> Weird.
>
> I will have to do some bench marks. I just thought that if there was already a string
> concatenation happening, that an additional concatenation would cost me twice as much
> time, but not 50-100 times. Plus, the second concatenation is a small concatenation.
> It's only two characters in length (#13#10) whereas the Line is much bigger.
The first string is always long, so you are doing twice adding a small
string to a long string. This causes much heap fragmentation.
Try result:= result + (Line + #13#10);
or
Line := Line + #13#10;
Result := Result + Line;
OTOH using a TStringList is probably faster, because strings are only
concatenated once.
Vicnent.
More information about the fpc-pascal
mailing list