Skip to content

Instantly share code, notes, and snippets.

@davepcallan
Created June 20, 2023 16:37
Show Gist options
  • Save davepcallan/c33482551f3d5bd1598866c661bf80af to your computer and use it in GitHub Desktop.
Save davepcallan/c33482551f3d5bd1598866c661bf80af to your computer and use it in GitHub Desktop.
.NET 8 - String concat with + and StringBuilder with PGO on v PGO off
using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;
namespace BenchmarkDotNet.Samples
{
[Config(typeof(Config))]
[HideColumns(Column.RatioSD, Column.AllocRatio, Column.Error, Column.StdDev, Column.Runtime)]
public class StringConcatDPGO
{
[Params(1000)]
public int Concats { get; set; }
[Benchmark]
public string Plus()
{
var result = string.Empty;
for (int i = 0; i < Concats; i++)
{
result = result + "1234567890";
}
return result;
}
[Benchmark]
public string SB_10()
{
StringBuilder result = new StringBuilder();
for (int i = 0; i < Concats; i++)
{
result.Append("1234567890");
}
return result.ToString();
}
private class Config : ManualConfig
{
public Config()
{
AddJob(Job.Default.WithId("Non DPGO")
.WithBaseline(true)
.WithRuntime(CoreRuntime.Core80)
.WithEnvironmentVariables(
new EnvironmentVariable("DOTNET_TieredPGO", "0")));
AddJob(Job.Default.WithId("DPGO")
.WithRuntime(CoreRuntime.Core80)
.WithEnvironmentVariables(
new EnvironmentVariable("DOTNET_TieredPGO", "1")));
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