[fpc-pascal] string concatenation speed

Leonhard Holz leonhard.holz at web.de
Tue Jun 21 21:19:53 CEST 2005


Hi,

> 
> 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.

> 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.

Leo




More information about the fpc-pascal mailing list