[fpc-pascal] string concatenation speed

Marc Santhoff M.Santhoff at t-online.de
Tue Jun 21 20:34:59 CEST 2005


Am Dienstag, den 21.06.2005, 19:46 +0200 schrieb Michael Van Canneyt:
> 
> On Tue, 21 Jun 2005, Marc Santhoff wrote:
> 
> > Hi,
> > 
> > in an application of mine occurs a lot of string separation and
> > re-concatenation. Since it is using masses of ANSI strings this is some
> > performance problem.
> > 
> > The strings get stored and loaded from  TStringList's and concatenated
> > by simply using '+' and separated with the function copy().
> > 
> > What I'd like to know is: What's are fastest ways of handling ANSI
> > strings?
> 
> It's hard to say in general; You would have to supply some example code.
> 
> if you're doing things like
>   For I:=X to Y do
>    S:=S+L[i];  // S string, L list.

Somewhat similar to that:

if (sl[4] <> '') then
		if ((sl[4][1] = '"') AND (sl[4][length(sl[4])] = '"'))
			then BaseColor := copy(sl[4],2,length(sl[4])-2) else BaseColor :=
sl[4];

where BaseColor is a field of a class-type object, sl is a TStringList.
I have to look for double quotes here and cut them out if necessary.

Sequences like this are heavily used in loading CSV files. This takes
rather long on a 'small' cpu (Geode 300MHz).

> then
>   It might be better to do
> 
>   Len:=0;
>   For I:=X to Y do
>     Inc(Len,Length(L[i]));  // S string, L list.
>   SetLength(S,Len);
>   P:=1;
>   For I:=X to Y do
>     begin
>     T:=L[i];
>     Len:=Length(T);
>     Move(T[1],S[P],Len);
>     inc(P,Len)
>     end;
> 
> This will avoid a lot of calls to uniquestring, get/setlength etc.

Another snippet esecially for concatenation is this:

result := 	IntToStr(ID) + SEP + 
		IntToStr(ID_Customer) + SEP +
		QT + Treatment + QT + SEP +
		DateToStr(Date) + SEP + 
		QT + BaseColor + QT + SEP +
... and so on for approx. 15 fields

I think I'll try your technique here ...

> Also, keep in mind that getting the I-th string from a list is an expensive operation.
> 
> So it is better to do
>    T:=L[i];
>    S:=Copy(T,X,Length(T)-X);
> than to do
>    S:=Copy(L[i],X,Length(L[i])-X);
> 
> The first option will call GetString only once, the second will call it twice.

This will help me, I'm sure. :)

Thank you so far,
Marc






More information about the fpc-pascal mailing list