Delphi StringBuilder

后端 未结 7 1811
别那么骄傲
别那么骄傲 2021-02-12 12:26

Exists in Delphi something like the Java or C# StringBuilder? Or Delphi does not need StringBuilder and s := s + \'some string\'; is good expression (mainly in for,

7条回答
  •  不要未来只要你来
    2021-02-12 13:11

    I'm really surprised that no one mentioned in any of the comments or examples that you can instruct TStringBuilder to preallocate a buffer appropriate to the task during instantiation. In other words, if you can come up with a simple estimate of how much memory you'll probably need, pad that a bit, and use that value to instantiate TStringBuilder , you avoid the memory reallocations that slow simple string concatenation to a crawl:

    buff := TStringBuilder.Create( tmpEstimatedSize );
    

    I use TStringBuilder regularly in new code and to optimize old code, and the CPU savings when building large strings incrementally is dramatic. Now to be transparent, if I just have a handful of strings to concatenate, I don't bother with TStringBuilder. But if I'm, say, serializing what could potentially be a large amount of data, TStringBuilder is the obvious solution.

提交回复
热议问题