-
Notifications
You must be signed in to change notification settings - Fork 273
/
SearchValuesCharTests.cs
63 lines (48 loc) · 2.03 KB
/
SearchValuesCharTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
using System.Runtime.CompilerServices;
namespace System.Buffers.Tests
{
[BenchmarkCategory(Categories.Runtime, Categories.Libraries, Categories.Span)]
public class SearchValuesCharTests
{
private const int InputLength = 256;
private SearchValues<char> _searchValues;
private char[] _text;
private char[] _textExcept;
[Params(
"abcdefABCDEF0123456789", // ASCII
"abcdefABCDEF0123456789Ü", // Mixed ASCII and non-ASCII
"ßäöüÄÖÜ" // Non-ASCII only
)]
public string Values;
[MethodImpl(MethodImplOptions.NoInlining)]
private char CharNotInSet() => '\n';
[GlobalSetup]
public void Setup()
{
_searchValues = SearchValues.Create(Values);
char charInSet = Values[0];
_text = new string(CharNotInSet(), InputLength).ToCharArray();
_textExcept = new string(charInSet, InputLength).ToCharArray();
_text[InputLength / 2] = charInSet;
_textExcept[InputLength / 2] = CharNotInSet();
}
[Benchmark]
public bool Contains() => _searchValues.Contains(CharNotInSet());
[Benchmark]
public bool ContainsAny() => _text.AsSpan().ContainsAny(_searchValues);
[Benchmark]
public int IndexOfAny() => _text.AsSpan().IndexOfAny(_searchValues);
[Benchmark]
[MemoryRandomization]
public int IndexOfAnyExcept() => _textExcept.AsSpan().IndexOfAnyExcept(_searchValues);
[Benchmark]
public int LastIndexOfAny() => _text.AsSpan().LastIndexOfAny(_searchValues);
[Benchmark]
public int LastIndexOfAnyExcept() => _textExcept.AsSpan().LastIndexOfAnyExcept(_searchValues);
}
}