Skip to content

Instantly share code, notes, and snippets.

@adamsitnik
Created September 23, 2019 09:12
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 adamsitnik/f79f0acb5c0f83c68517fd44fca5bf27 to your computer and use it in GitHub Desktop.
Save adamsitnik/f79f0acb5c0f83c68517fd44fca5bf27 to your computer and use it in GitHub Desktop.
params_valuetuples
public class StringHash
{
public static IEnumerable<(CultureInfo CultureInfo, CompareOptions CompareOptions)> GetOptions()
{
// Ordinal and OrdinalIgnoreCase use single execution path for all cultures, so we test it only for "en-US"
yield return (new CultureInfo("en-US"), CompareOptions.Ordinal);
yield return (new CultureInfo("en-US"), CompareOptions.OrdinalIgnoreCase);
yield return (new CultureInfo("en-US"), CompareOptions.None);
yield return (new CultureInfo("en-US"), CompareOptions.IgnoreCase);
yield return (CultureInfo.InvariantCulture, CompareOptions.None);
yield return (CultureInfo.InvariantCulture, CompareOptions.IgnoreCase);
}
[ParamsSource(nameof(GetOptions))]
public (CultureInfo CultureInfo, CompareOptions CompareOptions) Options;
[Params(
128, // small input that fits into stack-allocated array https://github.com/dotnet/coreclr/blob/c6675ef2e22474d6222d054ae3d022c01eda9b6d/src/System.Private.CoreLib/shared/System/Globalization/CompareInfo.Unix.cs#L824
1024 * 128)] // medium size input that fits into an array rented from ArrayPool.Shared without allocation
public int Count { get; set; }
private string _value;
[GlobalSetup] // we are using part of Alice's Adventures in Wonderland text as test data
public void Setup() => _value = new string(File.ReadAllText(CompressedFile.GetFilePath("alice29.txt")).Take(Count).ToArray());
[Benchmark]
public new void GetHashCode() => Options.CultureInfo.CompareInfo.GetHashCode(_value, Options.CompareOptions);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment