Skip to content

Commit

Permalink
Expanding benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
jtmueller committed May 17, 2024
1 parent cbe00c5 commit 1c8798e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
15 changes: 15 additions & 0 deletions src/RustyOptions.Benchmarks/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Reports;

namespace RustyOptions.Benchmarks;

internal sealed class Config : ManualConfig
{
public Config()
{
AddDiagnoser(MemoryDiagnoser.Default);
SummaryStyle = SummaryStyle.Default.WithRatioStyle(RatioStyle.Percentage);
}
}
17 changes: 10 additions & 7 deletions src/RustyOptions.Benchmarks/FirstOrNone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ namespace RustyOptions.Benchmarks;

[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
[CategoriesColumn]
[Config(typeof(Config))]
public class FirstOrNoneBenchmarks
{
const int SIZE = 1000;

private readonly int[] intArray;
private readonly List<int> intList;

public FirstOrNoneBenchmarks()
{
intArray = [.. Enumerable.Range(1, 1000)];
intArray = [.. Enumerable.Range(1, SIZE)];
intList = [.. intArray];
}

Expand Down Expand Up @@ -46,36 +49,36 @@ public void FirstOrNoneListOld()
_ = FirstOrNoneOld(intList);
}

private static bool IsEven(int x) => (x & 1) == 0;
private static bool IsHalfway(int x) => x == SIZE / 2;

[BenchmarkCategory("Predicate"), Benchmark]
public void FirstOrNoneWithPredicateEnumerable()
{
_ = IntEnumerable().FirstOrNone(IsEven);
_ = IntEnumerable().FirstOrNone(IsHalfway);
}

[BenchmarkCategory("Predicate"), Benchmark]
public void FirstOrNoneWithPredicateArray()
{
_ = intArray.FirstOrNone(IsEven);
_ = intArray.FirstOrNone(IsHalfway);
}

[BenchmarkCategory("Predicate"), Benchmark(Baseline = true)]
public void FirstOrNoneWithPredicateArrayOld()
{
_ = FirstOrNoneOld(intArray, IsEven);
_ = FirstOrNoneOld(intArray, IsHalfway);
}

[BenchmarkCategory("Predicate"), Benchmark]
public void FirstOrNoneWithPredicateList()
{
_ = intList.FirstOrNone(IsEven);
_ = intList.FirstOrNone(IsHalfway);
}

[BenchmarkCategory("Predicate"), Benchmark]
public void FirstOrNoneWithPredicateListOld()
{
_ = FirstOrNoneOld(intList, IsEven);
_ = FirstOrNoneOld(intList, IsHalfway);
}

private static Option<T> FirstOrNoneOld<T>(IEnumerable<T> items)
Expand Down
17 changes: 10 additions & 7 deletions src/RustyOptions.Benchmarks/LastOrNone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ namespace RustyOptions.Benchmarks;

[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
[CategoriesColumn]
[Config(typeof(Config))]
public class LastOrNoneBenchmarks
{
const int SIZE = 1000;

private readonly int[] intArray;
private readonly List<int> intList;

public LastOrNoneBenchmarks()
{
intArray = [.. Enumerable.Range(1, 1000)];
intArray = [.. Enumerable.Range(1, SIZE)];
intList = [.. intArray];
}

Expand Down Expand Up @@ -46,36 +49,36 @@ public void LastOrNoneListOld()
_ = LastOrNoneOld(intList);
}

private static bool IsEven(int x) => (x & 1) == 0;
private static bool IsHalfway(int x) => x == 500;

[BenchmarkCategory("Predicate"), Benchmark]
public void LastOrNoneWithPredicateEnumerable()
{
_ = IntEnumerable().LastOrNone(IsEven);
_ = IntEnumerable().LastOrNone(IsHalfway);
}

[BenchmarkCategory("Predicate"), Benchmark]
public void LastOrNoneWithPredicateArray()
{
_ = intArray.LastOrNone(IsEven);
_ = intArray.LastOrNone(IsHalfway);
}

[BenchmarkCategory("Predicate"), Benchmark(Baseline = true)]
public void LastOrNoneWithPredicateArrayOld()
{
_ = LastOrNoneOld(intArray, IsEven);
_ = LastOrNoneOld(intArray, IsHalfway);
}

[BenchmarkCategory("Predicate"), Benchmark]
public void LastOrNoneWithPredicateList()
{
_ = intList.LastOrNone(IsEven);
_ = intList.LastOrNone(IsHalfway);
}

[BenchmarkCategory("Predicate"), Benchmark]
public void LastOrNoneWithPredicateListOld()
{
_ = LastOrNoneOld(intList, IsEven);
_ = LastOrNoneOld(intList, IsHalfway);
}

private static Option<T> LastOrNoneOld<T>(IEnumerable<T> items)
Expand Down

0 comments on commit 1c8798e

Please sign in to comment.