Performance of using StringBuilder & string concatenation in a loop

Introduction

In this blog, we will be discussing the performance of using StringBuilder versus string concatenation in a loop. As programmers, we often need to work with large amounts of data and strings. When it comes to building strings in a loop, it is important to choose the most efficient method to avoid any performance issues or slow execution times.

StringBuilder is a powerful class in C# that can be used to efficiently manipulate strings, especially when dealing large amounts of data. On the other hand, string concatenation using the + operator can also be used to build strings in a loop, but it may not be as efficient as StringBuilder in certain scenarios.

StringBuilder in C#

  • StringBuilder is mutable.
  • It will only create one object on the heap.
  • Every time it would be updated with a new value even if you append/insert 1 million values.
  • Use it when you are going to perform an operation e.g. append/remove repetitively.

To learn more about C# Strings Builder in-depth, find this detailed article- C# StringBuilder Tutorial With Code Examples.

String in C#

  • It is immutable.
  • Every time we update data in a string, it creates a new instance of an object.
  • If you update the value 1K times, it will create 1K new instances.
  • Use strings when you aren't going to perform operations like(Append/Remove) repetitively. 

Learn more about C# Strings in-depth in the article- C# Strings: The Ultimate Guide to Go from Novice to Expert!

The below example shows the time taken duration when we perform 10K records in an iteration.

static void Main(string[] args)
        {
            Stopwatch mytimer = new Stopwatch();
            mytimer.Start();
            IEnumerable<int> numbers = Enumerable.Range(1, 10000).ToList();
            StringBuilder stringBuilder = new StringBuilder();
            foreach(var data in numbers)
            {
                if(data%2 == 0)
                {
                    stringBuilder.Append(($"{data} is even"));
                }
                else
                {
                    stringBuilder.Append(($"{data} is odd"));
                }
            }
            mytimer.Stop();
            Console.WriteLine("time taken by string builder is  {0} ", mytimer.Elapsed);

            Stopwatch mytimer_string = new Stopwatch();
            mytimer_string.Start();
            IEnumerable<int> number_string = Enumerable.Range(1, 10000).ToList();
            string data_strin = string.Empty;
            foreach (var data in number_string)
            {
                if (data % 2 == 0)
                {
                    data_strin = data_strin + (($"{data } is even"));
                    
                }
                else
                {
                    data_strin = data_strin + (($"{data } is odd"));
                    
                }
            }
            mytimer_string.Stop();
            Console.WriteLine("time taken by string  is  {0} ", mytimer_string.Elapsed);
        }

Time is taken for 10K iteration.

Time taken for 10k Iteration

Time is taken for 100K iterations.

Next Recommended Reading Reverse a String Using for Loop