[fpc-pascal] string concatenation speed
Marc Santhoff
M.Santhoff at t-online.de
Tue Jun 21 23:18:35 CEST 2005
Am Dienstag, den 21.06.2005, 21:19 +0200 schrieb Leonhard Holz:
> Hi,
Hi Leo,
> >
> > 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];
>
> You can tweak this a little by storing sl[4] and length(sl[4]) in local
> vars, but the whole approach tends to be slow. If you want it faster,
> dont allocate memory ("copy" & ":=" on the stack) and dont move string
> data. This could be done by "translating" BaseColor to an int or so.
> Either case you should not to use the StringList - read out BaseColor
> (and the other fields) directly from the source, skipping the " at reading.
I'm trying local temporary vars next, time measuring code is already in.
> > 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
>
> Same as above - try to avoid the concatenation. What do you do with
> result? If you write it to a file, write it directly. If you echo it
> somewhere, echo it directly. If you pass it to another function, make it
> a record with pointers to the string data.
Since I'm lazy and love the TStringList since Delphi 1 I do not write at
all myself but let the stringlist do it (saveToFile / loadFromFile). ;)
But I have to validate my habits anew in this case.
Thank you, I stand corrected,
Marc
More information about the fpc-pascal
mailing list