Skip to content

Commit 067f995

Browse files
Added benchmarks for IndexOfTests.
1 parent c5e88b1 commit 067f995

5 files changed

Lines changed: 86 additions & 13 deletions

File tree

Benchmarks/EnumAttributeTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Open.Text.Benchmarks;
44

5-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "<Pending>")]
65
public class EnumAttributeTests
76
{
87
public static IReadOnlyList<Attribute> GetAttribute(Greek value)

Benchmarks/EnumParseTests.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BenchmarkDotNet.Attributes;
2+
using CommandLine;
23
using FastEnumUtility;
34

45
namespace Open.Text.Benchmarks;
@@ -68,8 +69,9 @@ abstract class Tests
6869

6970
static readonly Dictionary<string, Greek> LookupD
7071
= Enum
71-
.GetValues<Greek>()
72-
.ToDictionary(e => Enum.GetName(e)!, e => e, StringComparer.Ordinal);
72+
.GetValues(typeof(Greek))
73+
.Cast<Greek>()
74+
.ToDictionary(e => e.GetName(), e => e, StringComparer.Ordinal);
7375

7476
protected virtual bool Lookup(string value, out Greek e)
7577
=> LookupD.TryGetValue(value, out e);
@@ -261,8 +263,9 @@ public override Greek FastEnumParse()
261263

262264
static readonly Dictionary<string, Greek> LookupD
263265
= Enum
264-
.GetValues<Greek>()
265-
.ToDictionary(e => Enum.GetName(e)!, e => e, StringComparer.OrdinalIgnoreCase);
266+
.GetValues(typeof(Greek))
267+
.Cast<Greek>()
268+
.ToDictionary(e => e.GetName(), e => e, StringComparer.OrdinalIgnoreCase);
266269

267270
protected override bool Lookup(string value, out Greek e)
268271
=> LookupD.TryGetValue(value, out e);
@@ -327,8 +330,9 @@ public override Greek FastEnumParse()
327330

328331
static readonly Dictionary<string, Greek> LookupD
329332
= Enum
330-
.GetValues<Greek>()
331-
.ToDictionary(e => Enum.GetName(e)!, e => e, StringComparer.OrdinalIgnoreCase);
333+
.GetValues(typeof(Greek))
334+
.Cast<Greek>()
335+
.ToDictionary(e => e.GetName(), e => e, StringComparer.OrdinalIgnoreCase);
332336

333337
protected override bool Lookup(string value, out Greek e)
334338
=> LookupD.TryGetValue(value, out e);

Benchmarks/IndexOfTests.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
}

Benchmarks/Open.Text.Benchmarks.csproj

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFrameworks>net472;net6.0;net8.0</TargetFrameworks>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
8+
<LangVersion>latest</LangVersion>
9+
<NoWarn>CA1822</NoWarn>
810
</PropertyGroup>
911

1012
<ItemGroup>
11-
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
12-
<PackageReference Include="FastEnum" Version="1.7.0" />
13+
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
14+
<PackageReference Include="FastEnum" Version="1.8.0" />
1315
</ItemGroup>
1416

1517
<ItemGroup>

Benchmarks/Program.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
using BenchmarkDotNet.Running;
22
using Open.Text.Benchmarks;
33

4+
//BenchmarkSwitcher
5+
// .FromAssembly(typeof(Program).Assembly)
6+
// .Run(args); // crucial to make it work
7+
48
//BenchmarkRunner.Run<EnumParseTests>();
5-
//enchmarkRunner.Run<EnumToStringTests>();
9+
//BenchmarkRunner.Run<EnumToStringTests>();
610
//BenchmarkRunner.Run<CharAssumptionTests>();
711
//BenchmarkRunner.Run<EnumAttributeTests>();
812
//BenchmarkRunner.Run<IsDefinedTests>();
9-
BenchmarkRunner.Run<StringConcatTests>();
13+
//BenchmarkRunner.Run<StringConcatTests>();
14+
BenchmarkRunner.Run<IndexOfTests>();

0 commit comments

Comments
 (0)