Skip to content

Instantly share code, notes, and snippets.

@davepcallan
Created January 6, 2023 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davepcallan/860cf8dde2103cab97a2a01ac82d8bcb to your computer and use it in GitHub Desktop.
Save davepcallan/860cf8dde2103cab97a2a01ac82d8bcb to your computer and use it in GitHub Desktop.
using System.Net.Http;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;
namespace BenchmarkDotNet.Samples
{
[Config(typeof(Config))]
[SimpleJob(RuntimeMoniker.Net50, baseline:true)]
[SimpleJob(RuntimeMoniker.Net60)]
[SimpleJob(RuntimeMoniker.Net70)]
[HideColumns(Column.Job, Column.RatioSD, Column.AllocRatio)]
public class StringReplaceBenchmarks
{
private string _str;
[GlobalSetup]
public async Task Setup()
{
using var hc = new HttpClient();
_str = await
hc.GetStringAsync("https://www.gutenberg.org/cache/epub/3200/pg3200.txt");
// The Entire Project Gutenberg Works of Mark Twain
}
[Benchmark]
public string Yell() => _str.Replace(".", "!");
[Benchmark]
public string ConcatLines() => _str.Replace("\n", "");
[Benchmark]
public string NormalizeEndings() => _str.Replace("\r\n", "\n");
[Benchmark]
public string Ireland() => _str.Replace("Ireland", "IRELAND"); //19 occurances
[Benchmark]
public string England() => _str.Replace("England", "ENGLAND"); //638 occurances
private class Config : ManualConfig
{
public Config()
{
SummaryStyle =
SummaryStyle.Default.WithRatioStyle(RatioStyle.Percentage);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment