Skip to content

Instantly share code, notes, and snippets.

@sonnemaf
Created February 15, 2023 15:55
Show Gist options
  • Save sonnemaf/1547b1f51dccfc10bb585cf6fb645e44 to your computer and use it in GitHub Desktop.
Save sonnemaf/1547b1f51dccfc10bb585cf6fb645e44 to your computer and use it in GitHub Desktop.
Performance benchmark for string interpolations
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Samples;
using System;
using System.Text;
//var s = new StringConcatSimple();
//Console.WriteLine(s.StringInterpolation());
//Console.WriteLine(s.StringJoin());
//Console.WriteLine(s.StringConcat());
//Console.WriteLine(s.StringCreate());
BenchmarkRunner.Run<StringConcatSimple>();
namespace BenchmarkDotNet.Samples {
//[SimpleJob(RuntimeMoniker.Net60, baseline: true)]
[SimpleJob(RuntimeMoniker.Net70)]
[HideColumns(Column.Job, Column.RatioSD, Column.AllocRatio)]
[MemoryDiagnoser]
public class StringConcatSimple {
private string title = "Mr.", firstName = "David", middleName = "Patrick", lastName = "Callan";
//[Benchmark]
//public string StringBuilder() {
// var stringBuilder =
// new StringBuilder();
// return stringBuilder
// .Append(title).Append(' ')
// .Append(firstName).Append(' ')
// .Append(middleName).Append(' ')
// .Append(lastName).ToString();
//}
//[Benchmark]
//public string StringBuilderExact24() {
// var stringBuilder =
// new StringBuilder(24);
// return stringBuilder
// .Append(title).Append(' ')
// .Append(firstName).Append(' ')
// .Append(middleName).Append(' ')
// .Append(lastName).ToString();
//}
//[Benchmark]
//public string StringBuilderEstimate100() {
// var stringBuilder =
// new StringBuilder(100);
// return stringBuilder
// .Append(title).Append(' ')
// .Append(firstName).Append(' ')
// .Append(middleName).Append(' ')
// .Append(lastName).ToString();
//}
//[Benchmark]
//public string StringPlus() {
// return title + ' ' + firstName + ' ' + middleName + ' ' + lastName;
//}
//[Benchmark]
//public string StringFormat() {
// return string.Format("{0} {1} {2} {3}", title, firstName, middleName, lastName);
//}
[Benchmark]
public string StringInterpolation() {
return $"{title} {firstName} {middleName} {lastName}";
}
[Benchmark]
public string StringJoin() {
return string.Join(' ', title, firstName, middleName, lastName);
}
[Benchmark]
public string StringConcat() {
return string.Concat(new String[] { title, " ", firstName, " ", middleName, " ", lastName });
}
[Benchmark]
public string StringCreate() {
return string.Create(length: title.Length + firstName.Length + middleName.Length + lastName.Length + 3, state: (title, firstName, middleName, lastName), action: (span, state) => {
state.title.CopyTo(span);
span[title.Length] = ' ';
span = span.Slice(title.Length + 1);
state.firstName.CopyTo(span);
span[firstName.Length] = ' ';
span = span.Slice(firstName.Length + 1);
state.middleName.CopyTo(span);
span[middleName.Length] = ' ';
span = span.Slice(middleName.Length + 1);
state.lastName.CopyTo(span);
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment