Skip to content

Commit

Permalink
Add Dictionary to the benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmada committed Oct 11, 2023
1 parent 25e2eb2 commit 0e5164e
Showing 1 changed file with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,36 +1,68 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;

namespace NetFabric.Hyperlinq.Analyzer.Benchmarks;

[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
[CategoriesColumn]
public class HLQ010_UseForLoop_ValueTypeEnumerator
{
ArraySegment<int> source;
ArraySegment<int> arraySegment;
Dictionary<int, int> dictionary;

[Params(100, 10_000)]
public int Count { get; set; }

[GlobalSetup]
public void GlobalSetup()
{
source = new ArraySegment<int>(Enumerable.Range(0, Count).ToArray());
var array = Enumerable.Range(0, Count).ToArray();
arraySegment = new ArraySegment<int>(array);
dictionary = array.ToDictionary(item => item);
}

[BenchmarkCategory("ArraySegment")]
[Benchmark(Baseline = true)]
public int Foreach()
public int ArraySegment_Foreach()
{
var sum = 0;
foreach (var item in source!)
foreach (var item in arraySegment!)
sum += item;
return sum;
}

[BenchmarkCategory("ArraySegment")]
[Benchmark]
public int For()
public int ArraySegment_For()
{
var sum = 0;
for (var index = 0; index < source!.Count; index++)
for (var index = 0; index < arraySegment!.Count; index++)
{
var item = source![index];
var item = arraySegment![index];
sum += item;
}
return sum;
}


[BenchmarkCategory("Dictionary")]
[Benchmark(Baseline = true)]
public int Dictionary_Foreach()
{
var sum = 0;
foreach (var item in dictionary!)
sum += item.Value;
return sum;
}

[BenchmarkCategory("Dictionary")]
[Benchmark]
public int Dictionary_For()
{
var sum = 0;
for (var index = 0; index < dictionary!.Count; index++)
{
var item = dictionary![index];
sum += item;
}
return sum;
Expand Down

0 comments on commit 0e5164e

Please sign in to comment.