Project: Benchmark String Concatenation vs StringBuilder
Context: This project benchmarks two common ways to concatenate many strings: using the + operator (which creates many intermediate strings) and using StringBuilder (which uses a mutable buffer). You will use BenchmarkDotNet with [MemoryDiagnoser] to measure both execution time and memory allocations. This demonstrates why StringBuilder is preferred for loops with many concatenations.
Usage Example
Section titled “Usage Example”using BenchmarkDotNet.Attributes;using BenchmarkDotNet.Running;using System.Text;
[MemoryDiagnoser]public class StringConcatBenchmark{ private const int Iterations = 1000;
[Benchmark] public string ConcatWithPlus() { string result = ""; for (int i = 0; i < Iterations; i++) { result += i.ToString(); } return result; }
[Benchmark] public string ConcatWithStringBuilder() { var sb = new StringBuilder(); for (int i = 0; i < Iterations; i++) { sb.Append(i); } return sb.ToString(); }}
class Program{ static void Main(string[] args) { var summary = BenchmarkRunner.Run<StringConcatBenchmark>(); }}Output console
Section titled “Output console”dotnet run -c Release| Method | Mean | Gen0 | Allocated ||------------------------ |---------:|---------:|----------:|| ConcatWithPlus | 845.2 us | 123.4567 | 512.34 KB || ConcatWithStringBuilder | 12.3 us | 0.1234 | 4.56 KB |How to run
Section titled “How to run”- Create a new console project.
- Add the BenchmarkDotNet NuGet package:
dotnet add package BenchmarkDotNet - Replace
Program.cswith the code above. - Run in Release mode:
dotnet run -c Release
Important notes
Section titled “Important notes”- The
+operator creates a new string each iteration (strings are immutable). StringBuilderuses an internal buffer that grows as needed, reducing allocations.
Real‑world usage example
Section titled “Real‑world usage example”Building large HTML/JSON strings – In a web API, using StringBuilder (or modern String.Create) is crucial for performance when constructing large responses.
See .NET docs on StringBuilder.