Skip to content

Instantly share code, notes, and snippets.

@davepcallan
Created July 31, 2023 09:24
Show Gist options
  • Save davepcallan/9214488b3c28bd4a55559fb459a0d099 to your computer and use it in GitHub Desktop.
Save davepcallan/9214488b3c28bd4a55559fb459a0d099 to your computer and use it in GitHub Desktop.
String equals benchmarks
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;
using System;
namespace BenchmarkDotNet.Samples
{
[MemoryDiagnoser]
[Config(typeof(Config))]
[SimpleJob(RuntimeMoniker.Net80)]
[HideColumns(Column.Job, Column.RatioSD, Column.AllocRatio)]
public class StringComparisonBenchmarks
{
private class Config : ManualConfig
{
public Config()
{
SummaryStyle =
SummaryStyle.Default.WithRatioStyle(RatioStyle.Percentage);
}
}
private string str1 = "HELLO WORLD";
private string str2 = "hello world";
[Benchmark(Baseline = true)]
public bool Equals_OrdinalIgnoreCase() => string.Equals(str1, str2, StringComparison.OrdinalIgnoreCase);
[Benchmark]
public bool Compare_OrdinalIgnoreCase() => string.Compare(str1, str2, StringComparison.OrdinalIgnoreCase) == 0;
[Benchmark]
public bool ToLower() => str1.ToLower() == str2.ToLower();
[Benchmark]
public bool ToUpper() => str1.ToUpper() == str2.ToUpper();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment