/stringConcatenationDotnet8.cs Secret
Last active
August 22, 2023 04:37
Star
You must be signed in to star a gist
.NET 8 simple string concatenation benchmarks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Columns; | |
using BenchmarkDotNet.Configs; | |
using BenchmarkDotNet.Jobs; | |
using BenchmarkDotNet.Reports; | |
using System; | |
using System.Text; | |
namespace Benchmarks | |
{ | |
[MemoryDiagnoser] | |
[Config(typeof(Config))] | |
[SimpleJob(RuntimeMoniker.Net80)] | |
[HideColumns(Column.Job, Column.RatioSD, Column.AllocRatio)] | |
public class StringConcatSimple | |
{ | |
private class Config : ManualConfig | |
{ | |
public Config() | |
{ | |
SummaryStyle = | |
SummaryStyle.Default.WithRatioStyle(RatioStyle.Percentage); | |
} | |
} | |
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(Baseline = true)] | |
public string StringCreate() | |
{ | |
return String.Create(title.Length + firstName.Length + middleName.Length + lastName.Length + 3, | |
(title, firstName, middleName, lastName), | |
(span, state) => | |
{ | |
state.title.AsSpan().CopyTo(span); | |
span = span.Slice(state.title.Length); | |
span[0] = ' '; | |
span = span.Slice(1); | |
state.firstName.AsSpan().CopyTo(span); | |
span = span.Slice(state.firstName.Length); | |
span[0] = ' '; | |
span = span.Slice(1); | |
state.middleName.AsSpan().CopyTo(span); | |
span = span.Slice(state.middleName.Length); | |
span[0] = ' '; | |
span = span.Slice(1); | |
state.lastName.AsSpan().CopyTo(span); | |
} | |
); | |
} | |
} | |
} |
Thanks David, I had no idea you could do this, it's a lot cleaner.
Very helpful thank you.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can also do string interpolation with the last example using TryWrite: