Text concatenation Best Practices and Performance implications: StringBuilder

Text concatenation Best Practices and Performance implications: StringBuilder

  

I just want to share with the community the importance of using StringBuilders when doing string concatenation, because of the performance boost that it gives.

The Text Extension (a System Component starting with 6.0) provides an API for using StringBuilders.


Simple string concatenation, "ab" + "cd", is ok when putting togheter some dynamic content to build a string, but should be completely avoided when processing big sets of data.

When you need to build massive strings or do multiple concatenations the StringBuilder really shines in terms of performance, when compared with simple concatenation approach.

However you have to be careful and ensure that you use the StringBuilder properly, look at the following code example:

Bad example of StringBuilder use

We're using a StringBuilder to concatenate a piece of text, so that means that we're being mindful of performance issues and we should get optimal performance from text operations, right?

Unfortunately no. Even though we are using the StringBuilder API we are still using simple concatenation in order to build the String parameter of the .Append() action. So in fact we're doing 1 optimized Append and 4 simple string concatenations, the gain here will be minimal.


What we should do is use StringBuilder.Append() explicitly to concatenate every single individual bit of text. The following example produces the same final string, using only optimized string concatenation.

Example of how to use the StringBuilder

If you think that this is irrelevant, let me share the real world example that motivated me to write this post.

I had an action that took a record list and serialized the structures into text, the serialized data would then be written into a file.
Using an approach like in the first image, the serialization of the data took about 8 minutes to produce a 9MB text output.

When we changed the code in order to use only calls to StringBuilder.Append(), like in the second image, the serialization of the same data took under 30 seconds.

This is a best practice that shouldn't be new to anyone with some .Net programming experience, but I felt it was important to share it, since a small and common thing like string concatenation can have such big impact on performance.

Hope that this might be helpful to others.

Goncalo,

+1, this should be made a HowTo performance post.

Thanks for sharing, I have some actions where I would really want to test it......now only find the time to change it :).

Kind regards,
Evert
Evert van der zalm wrote:
Goncalo,

+1, this should be made a HowTo performance post.

Thanks for sharing, I have some actions where I would really want to test it......now only find the time to change it :).

Kind regards,
Evert
 
I totally agree with you Evert.

It would be nice, to see Agile Platform How-to's/Exercices with more tips and more (hand's on/technical) best practices.
Definitely my like, Gonçalo. Thanks for sharing!

Daniel: thank you for asking. By the way, have you heard about the Performance Best Practices technote?