Noticable is relative. However: string interpolation is turned into string.Format()
at compile-time so they should end up with the same result.
There are subtle differences though: as we can tell from this question, string concatenation in the format specifier results in an additional string.Concat()
call.
The answer is both yes and no. ReSharper
is fooling you by not showing a third variant, which is also the most performant. The two listed variants produce equal IL code, but the following will indeed give a boost:
myString += $"{x.ToString("x2")}";
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Diagnostics.Windows;
using BenchmarkDotNet.Running;
namespace StringFormatPerformanceTest
{
[Config(typeof(Config))]
public class StringTests
{
private class Config : ManualConfig
{
public Config() => AddDiagnoser(MemoryDiagnoser.Default, new EtwProfiler());
}
[Params(42, 1337)]
public int Data;
[Benchmark] public string Format() => string.Format("{0:x2}", Data);
[Benchmark] public string Interpolate() => $"{Data:x2}";
[Benchmark] public string InterpolateExplicit() => $"{Data.ToString("x2")}";
}
class Program
{
static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<StringTests>();
}
}
}
| Method | Data | Mean | Gen 0 | Allocated |
|-------------------- |----- |----------:|-------:|----------:|
| Format | 42 | 118.03 ns | 0.0178 | 56 B |
| Interpolate | 42 | 118.36 ns | 0.0178 | 56 B |
| InterpolateExplicit | 42 | 37.01 ns | 0.0102 | 32 B |
| Format | 1337 | 117.46 ns | 0.0176 | 56 B |
| Interpolate | 1337 | 113.86 ns | 0.0178 | 56 B |
| InterpolateExplicit | 1337 | 38.73 ns | 0.0102 | 32 B |
The InterpolateExplicit()
method is faster since we now explicitly tell the compiler to use a string
. No need to box the object to be formatted. Boxing is indeed very costly. Also, note that we reduced the allocations a bit.
string interpolation is turned into string.Format() at compile-time.
Also in string.Format you can specify several outputs for single argument, and different output formats for single argument. But string interpolation is more readable I guess. So, it's up to you.
a = string.Format("Due date is {0:M/d/yy} at {0:h:mm}", someComplexObject.someObject.someProperty);
b = $"Due date is {someComplexObject.someObject.someProperty:M/d/yy} at {someComplexObject.someObject.someProperty:h:mm}";
There is some performance test results https://koukia.ca/string-interpolation-vs-string-format-string-concat-and-string-builder-performance-benchmarks-c1dad38032a
The question was about performance, however the title just says "vs", so I feel like have to add a few more points, some of them are opinionated though.
Localization
string.Format
. However, there is tooling for that (e.g. ReSharper
).Maintainability (my opinion)
string.Format
is far more readable, as it focuses on the sentence what I'd like to phrase, for example when constructing a nice and meaningful error message. Using the {N}
placeholders give me more flexibility and it's easier to modify it later.string.Format
is much less prone to this.So based on these I decided to stick with string.Format
in most of my code. However, I've prepared an extension method to have a more fluent way of coding which I like much more. The extension's implementaiton is a one-liner, and it looks simply like this in use.
var myErrorMessage = "Value must be less than {0:0.00} for field {1}".FormatWith(maximum, fieldName);
Interpolation is a great feature, don't get me wrong. But IMO it shines the best in those languages which miss the string.Format
-like feature, for example JavaScript.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With