Last active
July 3, 2023 16:28
-
-
Save SwapnilGaikwad/afaa66653ee1aa53b88e6f18c3cba7c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using BenchmarkDotNet.Attributes; | |
using System; | |
using System.Linq; | |
using System.Buffers; | |
using System.Text; | |
using MicroBenchmarks; | |
namespace StringSearcher; | |
[BenchmarkCategory(Categories.Libraries)] | |
public class IndexOfAnyBenchmarks | |
{ | |
private byte[] _targets = null!; | |
private byte[] _byteBuffer = null!; | |
[Params(1024)] | |
public int length; | |
[GlobalSetup] | |
public void Setup() | |
{ | |
var rnd = new Random(42); | |
byte[] a = new byte[length]; | |
int expectedIndex = length / 2; | |
for (int i = 0; i < length; i++) | |
{ | |
if (i == expectedIndex) | |
{ | |
continue; | |
} | |
a[i] = 255; | |
} | |
_byteBuffer = a; | |
_targets = new byte[length * 2]; | |
for (int i = 0; i < _targets.Length; i++) | |
{ | |
if (i == length + 1) | |
{ | |
continue; | |
} | |
_targets[i] = (byte)rnd.Next(1, 255); | |
} | |
} | |
public ReadOnlySpan<byte> values => _targets; | |
public Span<byte> span => new Span<byte>(_byteBuffer); | |
[Benchmark] public int MyBench() => LastIndexOfAny(span, values); | |
private static int LastIndexOfAny(Span<byte> span, ReadOnlySpan<byte> values) | |
{ | |
return span.LastIndexOfAny(SearchValues.Create(values)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment