Skip to content

Instantly share code, notes, and snippets.

@bdach
Created February 21, 2024 10:13
Show Gist options
  • Save bdach/97d06de3531a5b4821fe3acf39618d3e to your computer and use it in GitHub Desktop.
Save bdach/97d06de3531a5b4821fe3acf39618d3e to your computer and use it in GitHub Desktop.
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using BenchmarkDotNet.Attributes;
namespace osu.Game.Benchmarks
{
public class BenchmarkStringComparison
{
private string[] strings = null!;
[Params(StringComparison.Ordinal, StringComparison.OrdinalIgnoreCase, StringComparison.InvariantCulture)]
public StringComparison ComparisonMode { get; set; }
[GlobalSetup]
public void GlobalSetUp()
{
strings = new string[10000];
for (int i = 0; i < strings.Length; ++i)
strings[i] = Guid.NewGuid().ToString();
for (int i = 0; i < strings.Length; ++i)
{
if (i % 2 == 0)
strings[i] = strings[i].ToUpperInvariant();
}
}
[Benchmark]
public void TestSorting()
{
for (int i = 0; i < strings.Length; ++i)
{
for (int j = i + 1; j < strings.Length; ++j)
_ = string.Compare(strings[i], strings[j], ComparisonMode);
}
}
}
}
Method ComparisonMode Mean Error StdDev
TestSorting InvariantCulture 954.5 ms 18.25 ms 17.07 ms
TestSorting Ordinal 110.8 ms 1.27 ms 1.19 ms
TestSorting OrdinalIgnoreCase 187.6 ms 3.32 ms 3.10 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment