Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String Comparers benchmarks #535

Merged
merged 7 commits into from
Jun 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/benchmarks/micro/corefx/System.Runtime/Perf.StringComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Extensions;
using MicroBenchmarks;

namespace System.Tests
{
[BenchmarkCategory(Categories.CoreFX, Categories.CoreCLR)]
public class Perf_StringComparer
{
[Params(
128, // stackalloc path
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the covered cases :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

**Thanks for the comments too

1024 * 256, // ArrayPool.Shared.Rent without allocation
2 * 1024 * 1024)] // ArrayPool.Shared.Rent WITH allocation
public int Count { get; set; }

[Params(
StringComparison.Ordinal, StringComparison.OrdinalIgnoreCase,
StringComparison.InvariantCulture, StringComparison.InvariantCultureIgnoreCase)]
public StringComparison Comparison { get; set; }

private string _input, _same;
private StringComparer _comparer;

[GlobalSetup]
public void Setup()
{
_comparer = GetStringComparer();
char[] characters = ValuesGenerator.Array<char>(Count);
_input = new string(characters);
_same = new string(characters);
}

[Benchmark]
public int GetStringHashCode() => _comparer.GetHashCode(_input);

[Benchmark]
public int CompareSame() => _comparer.Compare(_input, _same);

private StringComparer GetStringComparer()
{
switch (Comparison)
{
case StringComparison.CurrentCulture:
return StringComparer.CurrentCulture;
case StringComparison.CurrentCultureIgnoreCase:
return StringComparer.CurrentCultureIgnoreCase;
case StringComparison.InvariantCulture:
return StringComparer.InvariantCulture;
case StringComparison.InvariantCultureIgnoreCase:
return StringComparer.InvariantCultureIgnoreCase;
case StringComparison.Ordinal:
return StringComparer.Ordinal;
case StringComparison.OrdinalIgnoreCase:
return StringComparer.OrdinalIgnoreCase;
default:
throw new NotSupportedException($"{Comparison} is not supported");
}
}
}
}