From 0e5164e3fd366f90aad1b6cf669084212702a168 Mon Sep 17 00:00:00 2001 From: Antao Almada Date: Wed, 11 Oct 2023 17:26:48 +0100 Subject: [PATCH] Add Dictionary to the benchmark --- .../HLQ010_UseForLoop.ValueTypeEnumerator.cs | 46 ++++++++++++++++--- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/NetFabric.Hyperlinq.Analyzer.Benchmarks/HLQ010_UseForLoop.ValueTypeEnumerator.cs b/NetFabric.Hyperlinq.Analyzer.Benchmarks/HLQ010_UseForLoop.ValueTypeEnumerator.cs index cce3e65..82ca702 100644 --- a/NetFabric.Hyperlinq.Analyzer.Benchmarks/HLQ010_UseForLoop.ValueTypeEnumerator.cs +++ b/NetFabric.Hyperlinq.Analyzer.Benchmarks/HLQ010_UseForLoop.ValueTypeEnumerator.cs @@ -1,10 +1,14 @@ using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Configs; namespace NetFabric.Hyperlinq.Analyzer.Benchmarks; +[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)] +[CategoriesColumn] public class HLQ010_UseForLoop_ValueTypeEnumerator { - ArraySegment source; + ArraySegment arraySegment; + Dictionary dictionary; [Params(100, 10_000)] public int Count { get; set; } @@ -12,25 +16,53 @@ public class HLQ010_UseForLoop_ValueTypeEnumerator [GlobalSetup] public void GlobalSetup() { - source = new ArraySegment(Enumerable.Range(0, Count).ToArray()); + var array = Enumerable.Range(0, Count).ToArray(); + arraySegment = new ArraySegment(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;