Creating Strings Best Practices?

NevaranNevaran BGMember ✭✭

I've seen a lot of ways of creating, combining, and adding strings. But which one is the best performance-wise?

In my code I use both StringBuilder.Append, and inside it I have a $" text {variable} "

Is it better to do StringBuilder.Append multiple times or like this:
sb.Append($"Speed: {speed}kmh");

I need as much performance as possible, since im doing a lot of these strings that build up to an information TextView in the end. And each string appending is modular.

Best Answer

  • NevaranNevaran BGMember ✭✭
    edited December 2017 Accepted Answer

    After testing with 3 different ways using StringBuilder, ive found this:

    Using an all-in StringBuilder.Append for each thing you want to add, seems to be the most unreliable and slowest, reaching 510ms to complete. Though it also sometimes drops the lowest of the three tests, down to ~220ms, although very rarely.

    Using a combination of Stringbuilder.AppendLine() and + operations inside it, it resulted in a ~440ms ** to complete the action. And a healthy dose of it going down to **~390ms, rarely to 280ms.
    Its better than the first one, but the code became hard to manage, especially if there were variables put in the lines.

    The last test was using the SB.Append() with $"" ,which I have been using at the start.
    When using the $"" for the line, and StringBuilder to merge the lines. The result was the best out of all three, going at a steady speed of 410ms and often in the** lower 300**. This code was the easiest to manage and edit, though it had some random slowdowns up to 500ms.

    All these tests were made having empty lines, pure strings, and strings with variables appended to one SB - in a for loop where the SB was being cleared and set 100 times.

    Hope this helped someone. And I hope someone might have a faster code to showoff!

Answers

  • NevaranNevaran BGMember ✭✭
    edited December 2017 Accepted Answer

    After testing with 3 different ways using StringBuilder, ive found this:

    Using an all-in StringBuilder.Append for each thing you want to add, seems to be the most unreliable and slowest, reaching 510ms to complete. Though it also sometimes drops the lowest of the three tests, down to ~220ms, although very rarely.

    Using a combination of Stringbuilder.AppendLine() and + operations inside it, it resulted in a ~440ms ** to complete the action. And a healthy dose of it going down to **~390ms, rarely to 280ms.
    Its better than the first one, but the code became hard to manage, especially if there were variables put in the lines.

    The last test was using the SB.Append() with $"" ,which I have been using at the start.
    When using the $"" for the line, and StringBuilder to merge the lines. The result was the best out of all three, going at a steady speed of 410ms and often in the** lower 300**. This code was the easiest to manage and edit, though it had some random slowdowns up to 500ms.

    All these tests were made having empty lines, pure strings, and strings with variables appended to one SB - in a for loop where the SB was being cleared and set 100 times.

    Hope this helped someone. And I hope someone might have a faster code to showoff!

Sign In or Register to comment.