Delphi: Fast(er) widestring concatenation
Posted
by Ian Boyd
on Stack Overflow
See other posts from Stack Overflow
or by Ian Boyd
Published on 2010-06-09T15:15:33Z
Indexed on
2010/06/10
13:42 UTC
Read the original article
Hit count: 376
i have a function who's job is to convert an ADO Recordset
into html:
class function RecordsetToHtml(const rs: _Recordset): WideString;
And the guts of the function involves a lot of wide string concatenation:
while not rs.EOF do
begin
Result := Result+CRLF+
'<TR>';
for i := 0 to rs.Fields.Count-1 do
Result := Result+'<TD>'+VarAsString(rs.Fields[i].Value)+'</TD>';
Result := Result+'</TR>';
rs.MoveNext;
end;
With a few thousand results, the function takes, what any user would feel, is too long to run. The Delphi Sampling Profiler shows that 99.3% of the time is spent in widestring concatenation (@WStrCatN
and @WstrCat
).
Can anyone think of a way to improve widestring concatenation? i don't think Delphi 5 has any kind of string builder. And Format
doesn't support Unicode.
And to make sure nobody tries to weasel out: pretend you are implementing the interface:
IRecordsetToHtml = interface(IUnknown)
function RecordsetToHtml(const rs: _Recordset): WideString;
end;
Update One
I thought of using an IXMLDOMDocument
, to build up the HTML as xml. But then i realized that the final HTML would be xhtml
and not html
- a subtle, but important, difference.
Update Two
Microsoft knowledge base article: How To Improve String Concatenation Performance
© Stack Overflow or respective owner