Performance: string concatenation vs String.Format vs interpolated string

 
 
  • Gérald Barré

After writing my previous post about interpolated strings in C#, I wondered what the most performant way to concatenate strings is. I used BenchmarkDotNet to find out.

Here's the benchmark code:

C#
// Full code: https://gist.github.com/meziantou/b120126088457f5f87ad200f4f7bf6b0
class Program
{
    static void Main(string[] args)
    {
        BenchmarkRunner.Run<StringBenchmark>();
    }
}

[OrderProvider(SummaryOrderPolicy.FastestToSlowest)]
[CoreJob]
[MemoryDiagnoser]
class StringBenchmark
{
    [Benchmark]
    public string StringConcatenation1() => "test" + 0 + "test0";

    [Benchmark]
    public string StringFormat1() => string.Format("test{0}{1}", 0, "test0");

    [Benchmark]
    public string StringInterpolation1() => $"test{0}{"test0"}"; // Should be the same as StringFormat

    [Benchmark]
    public string FormattableString1() => Format($"test{0}{"test0"}"); // Force the creation of a FormattableString

    // ...
    // same tests with 5, 10, 20, 50 100 arguments

    private static string Format(FormattableString fs) => fs.ToString();
}

| Method | Mean | Error | StdDev | Gen 0 | Allocated | |———————– |————-😐———–😐————😐——-😐———-😐 | StringConcatenation1 | 82.39 ns | 1.732 ns | 1.8534 ns | 0.0330 | 104 B | | StringFormat1 | 176.44 ns | 1.034 ns | 0.9674 ns | 0.0329 | 104 B | | StringInterpolation1 | 177.31 ns | 1.157 ns | 1.0824 ns | 0.0329 | 104 B | | FormattableString1 | 229.26 ns | 4.654 ns | 6.9661 ns | 0.0558 | 176 B | | | | | | | | | StringConcatenation5 | 453.13 ns | 2.980 ns | 2.4887 ns | 0.1903 | 600 B | | FormattableString5 | 661.04 ns | 17.338 ns | 23.1458 ns | 0.1621 | 512 B | | StringFormat5 | 688.18 ns | 7.096 ns | 6.2907 ns | 0.1516 | 480 B | | StringInterpolation5 | 692.05 ns | 3.329 ns | 2.9508 ns | 0.1516 | 480 B | | | | | | | | | StringConcatenation10 | 899.42 ns | 15.189 ns | 13.4643 ns | 0.3500 | 1104 B | | FormattableString10 | 1,160.01 ns | 9.950 ns | 8.3090 ns | 0.2956 | 936 B | | StringInterpolation10 | 1,317.73 ns | 9.445 ns | 8.3726 ns | 0.2861 | 904 B | | StringFormat10 | 1,330.47 ns | 9.240 ns | 8.6433 ns | 0.2861 | 904 B | | | | | | | | | StringConcatenation20 | 1,794.73 ns | 7.335 ns | 6.1252 ns | 0.6809 | 2144 B | | FormattableString20 | 2,260.88 ns | 22.249 ns | 18.5790 ns | 0.9003 | 2840 B | | StringInterpolation20 | 2,625.90 ns | 2.727 ns | 1.9720 ns | 0.8888 | 2808 B | | StringFormat20 | 2,642.51 ns | 20.843 ns | 18.4764 ns | 0.8888 | 2808 B | | | | | | | | | StringConcatenation50 | 4,394.86 ns | 86.688 ns | 106.4604 ns | 1.6708 | 5264 B | | FormattableString50 | 5,427.57 ns | 35.227 ns | 32.9509 ns | 2.1973 | 6920 B | | StringFormat50 | 6,313.41 ns | 32.824 ns | 27.4097 ns | 2.1820 | 6888 B | | StringInterpolation50 | 6,358.77 ns | 50.488 ns | 42.1595 ns | 2.1820 | 6888 B | | | | | | | | | StringConcatenation100 | 9,011.67 ns | 62.487 ns | 58.4504 ns | 3.3112 | 10464 B | | FormattableString100 | 10,941.37 ns | 88.319 ns | 78.2928 ns | 4.4098 | 13920 B | | StringFormat100 | 12,721.01 ns | 49.627 ns | 41.4407 ns | 4.4098 | 13888 B | | StringInterpolation100 | 12,924.46 ns | 329.898 ns | 392.7199 ns | 4.4098 | 13888 B |

All 4 methods perform similarly. However, string concatenation is noticeably faster when working with a very small number of arguments.

Do you have a question or a suggestion about this post? Contact me!

Follow me:
Enjoy this blog?