Simple string concatenation using String.Create and other performance sensitive approaches in .NET 8

I posted my previous simple string concatenation benchmark results where I was concatenating my name with spacer on Reddit recently.


I got some really interesting responses in the comments regarding alternative ways of using String.Create and alternative approaches to performance sensitive string concatenation in general.

Here’s the String.Create method I benchmarked


OK it’s not very simple at all but I wanted to include it with the regular Interpolation, + Concat operator, String.Join etc. approaches for comparative purposes.

I didn’t know we could reduce this to be a bit neater but on the Reddit thread David Fowler from Microsoft showed me an alternative using TryWrite …

Click the image for a larger view in a new window …


I also got a lot of suggestions from jnyrup (u/jnyrup) – Reddit with different variations of String.Create and some other approaches for example using UnsafeAssessor which is new in .NET 8 (Preview 6).

I put them altogether and here’s the benchmark results I got ⬇ …

All the benchmark methods are not really like for like so I split them into three categories where we don’t know the final string length (24), we do know the final string length and where we know the final string length and the length of the individual strings to concat.

Click on the image for larger view in a new window …


What do you think?
Any surprises here?

Although the original String.Create is faster the TryWrite version is a lot neater and I think is a good trade off.

If you know the lengths of all the strings to be concatenated and you absolutely need to save every ns consider benchmarking methods like below …

Benchmark your own specific scenario – don’t generalize these results

I kind of just post these benchmarks for fun. Don’t generalize them and expect the same results for different inputs. You need to test your own specific scenarios but this post has hopefully given you some ideas about what to test.

Benchmark code to recreate

Performance Sensitive simple string concatenation in .NET 8 (github.com)

Original benchmarks ->
.NET 8 simple string concatenation benchmarks (github.com)

Leave a Reply

Your email address will not be published. Required fields are marked *