Skip to content

Instantly share code, notes, and snippets.

@davepcallan
Created June 17, 2023 14:14
Show Gist options
  • Save davepcallan/adcfc82cfc0edda1ce720d888f5fa649 to your computer and use it in GitHub Desktop.
Save davepcallan/adcfc82cfc0edda1ce720d888f5fa649 to your computer and use it in GitHub Desktop.
StringBuilder Append Benchmark for .NET 7 v .NET 8
using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;
namespace BenchmarkDotNet.Samples
{
[Config(typeof(Config))]
[SimpleJob(RuntimeMoniker.Net80)]
[SimpleJob(RuntimeMoniker.Net70, baseline: true)]
[HideColumns(Column.RatioSD, Column.AllocRatio, Column.Error, Column.StdDev, Column.Runtime)]
public class StringBuilderAppend
{
[Params(100)]
public int Concats { get; set; }
[Benchmark]
public string SB_10()
{
StringBuilder result = new StringBuilder();
for (int i = 0; i < Concats; i++)
{
result.Append("1234567890");
}
return result.ToString();
}
[Benchmark]
public string SB_100()
{
StringBuilder result = new StringBuilder();
for (int i = 0; i < Concats; i++)
{
result.Append("khtFRrQOx0lsxNOSJuoyon2NE2csvMRkhZ9OqnrLQe63UIPlMu3Eyn2iWZh7UDfT8wrK5t2K8vOO2JUk4ymi9l0K4zNXD5KqkzVy");
}
return result.ToString();
}
private class Config : ManualConfig
{
public Config()
{
SummaryStyle =
SummaryStyle.Default.WithRatioStyle(RatioStyle.Percentage)
.WithTimeUnit(Perfolizer.Horology.TimeUnit.Millisecond);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment