-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
Processing dotnet/runtime#122749 (comment) command:
Command
-amd -intel -arm
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
using System.Buffers;
BenchmarkSwitcher.FromAssembly(typeof(TwoStringSearchValuesBenchmark).Assembly).Run(args);
[MemoryDiagnoser]
public class TwoStringSearchValuesBenchmark
{
private string _haystackBestCase = default!;
private string _haystackWorstCase = default!;
private SearchValues<string> _twoStringsOrdinal = default!;
private SearchValues<string> _twoStringsIgnoreCase = default!;
private SearchValues<string> _worstCaseOrdinal = default!;
[Params(100, 1000, 10000)]
public int HaystackLength { get; set; }
[Params("Early", "Middle", "Late", "None")]
public string MatchPosition { get; set; } = default!;
[GlobalSetup]
public void Setup()
{
Random rand = new(42);
// Best case: haystack has no 'h' or 'w' (first chars of "hello"/"world")
char[] charsBest = new char[HaystackLength];
for (int i = 0; i < charsBest.Length; i++)
charsBest[i] = (char)('m' + rand.Next(10)); // m-v range, no h or w
// Worst case: haystack has many 'a's (first char of both needles)
char[] charsWorst = new char[HaystackLength];
for (int i = 0; i < charsWorst.Length; i++)
charsWorst[i] = rand.Next(4) == 0 ? 'a' : (char)('b' + rand.Next(10)); // 25% 'a's
string needle1Best = "hello";
string needle2Best = "world";
string needle1Worst = "abc";
string needle2Worst = "axy";
int insertPos = MatchPosition switch
{
"Early" => 10,
"Middle" => HaystackLength / 2,
"Late" => HaystackLength - 10,
_ => -1
};
if (insertPos >= 0 && insertPos + needle1Best.Length <= HaystackLength)
{
needle1Best.AsSpan().CopyTo(charsBest.AsSpan(insertPos));
needle1Worst.AsSpan().CopyTo(charsWorst.AsSpan(insertPos));
}
_haystackBestCase = new string(charsBest);
_haystackWorstCase = new string(charsWorst);
_twoStringsOrdinal = SearchValues.Create([needle1Best, needle2Best], StringComparison.Ordinal);
_twoStringsIgnoreCase = SearchValues.Create([needle1Best, needle2Best], StringComparison.OrdinalIgnoreCase);
_worstCaseOrdinal = SearchValues.Create([needle1Worst, needle2Worst], StringComparison.Ordinal);
}
[Benchmark]
public int BestCase_Ordinal() => _haystackBestCase.AsSpan().IndexOfAny(_twoStringsOrdinal);
[Benchmark]
public int BestCase_IgnoreCase() => _haystackBestCase.AsSpan().IndexOfAny(_twoStringsIgnoreCase);
[Benchmark]
public int WorstCase_Ordinal() => _haystackWorstCase.AsSpan().IndexOfAny(_worstCaseOrdinal);
}(EgorBot will reply in this issue)
Metadata
Metadata
Assignees
Labels
No labels