|
| 1 | +using BenchmarkDotNet.Attributes; |
| 2 | +using BenchmarkDotNet.Jobs; |
| 3 | + |
| 4 | +namespace Open.Text.Benchmarks; |
| 5 | + |
| 6 | +[MemoryDiagnoser] |
| 7 | +[SimpleJob(RuntimeMoniker.Net472, baseline: true)] |
| 8 | +[SimpleJob(RuntimeMoniker.Net60)] |
| 9 | +[SimpleJob(RuntimeMoniker.Net80)] |
| 10 | +public class IndexOfTests |
| 11 | +{ |
| 12 | + public const string Text = "The quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy dog"; |
| 13 | + public static readonly string TextUpper = Text.ToUpper(); |
| 14 | + public const string Search = "fox"; |
| 15 | + public const string SearchCased = "Fox"; |
| 16 | + |
| 17 | + private StringComparison _comparison = StringComparison.Ordinal; |
| 18 | + private StringComparison _comparisonCaseIgnored; |
| 19 | + |
| 20 | + [Params( |
| 21 | + StringComparison.Ordinal, |
| 22 | + StringComparison.CurrentCulture, |
| 23 | + StringComparison.InvariantCulture)] |
| 24 | + public StringComparison Comparison |
| 25 | + { |
| 26 | + get => _comparison; |
| 27 | + set |
| 28 | + { |
| 29 | + _comparison = value; |
| 30 | + _comparisonCaseIgnored = _comparison switch |
| 31 | + { |
| 32 | + StringComparison.Ordinal => StringComparison.OrdinalIgnoreCase, |
| 33 | + StringComparison.CurrentCulture => StringComparison.CurrentCultureIgnoreCase, |
| 34 | + StringComparison.InvariantCulture => StringComparison.InvariantCultureIgnoreCase, |
| 35 | + _ => throw new ArgumentOutOfRangeException(nameof(value), value, null) |
| 36 | + }; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + [Benchmark(Baseline = true)] |
| 41 | + public int IndexOf() |
| 42 | + => Text.IndexOf(Search, _comparison); |
| 43 | + |
| 44 | + [Benchmark] |
| 45 | + public int IndexOfCaseIgnored() |
| 46 | + => Text.IndexOf(SearchCased, _comparisonCaseIgnored); |
| 47 | + |
| 48 | + [Benchmark] |
| 49 | + public int IndexOfSpan() |
| 50 | + => Text.IndexOf(Search.AsSpan(), _comparison); |
| 51 | + |
| 52 | + [Benchmark] |
| 53 | + public int IndexOfSpanCaseIgnored() |
| 54 | + => Text.IndexOf(SearchCased.AsSpan(), _comparisonCaseIgnored); |
| 55 | + |
| 56 | + [Benchmark] |
| 57 | + public int IndexOfSpanSlice() |
| 58 | + => Text.IndexOf(Text.AsSpan(16, 3), _comparison); |
| 59 | + |
| 60 | + [Benchmark] |
| 61 | + public int IndexOfSpanSliceCaseIgnored() |
| 62 | + => Text.IndexOf(TextUpper.AsSpan(16, 3), _comparisonCaseIgnored); |
| 63 | +} |
0 commit comments