From a1c1a80f101383ca62fad10320dbf0c1338ad234 Mon Sep 17 00:00:00 2001 From: Emmanuel ANDRE <2341261+manandre@users.noreply.github.com> Date: Thu, 12 Oct 2023 21:05:43 +0200 Subject: [PATCH 1/3] Add CountBy benchmarks --- src/benchmarks/micro/runtime/Linq/Linq.cs | 53 ++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/src/benchmarks/micro/runtime/Linq/Linq.cs b/src/benchmarks/micro/runtime/Linq/Linq.cs index bf8ce879d96..8513dff8900 100644 --- a/src/benchmarks/micro/runtime/Linq/Linq.cs +++ b/src/benchmarks/micro/runtime/Linq/Linq.cs @@ -118,7 +118,8 @@ public class LinqBenchmarks public const int IterationsWhere01 = 250000; public const int IterationsCount00 = 1000000; public const int IterationsOrder00 = 25000; - + public const int IterationsCountBy00 = 1000000; + #region Where00 [Benchmark] @@ -356,4 +357,54 @@ public bool Order00ManualX() return (medianPricedProduct.ProductID == 57); } #endregion + + #region CountBy00 + +#if NET9_0_OR_GREATER + [Benchmark] + public bool CountBy00LinqMethodX() + { + List products = Product.GetProductList(); + int count = 0; + for (int i = 0; i < IterationsCount00; i++) + { + count += products.CountBy(p => p.Category).Count(); + } + + return (count == 5 * IterationsCountBy00); + } +#endif + + [Benchmark] + public bool CountBy00GroupByX() + { + List products = Product.GetProductList(); + int count = 0; + for (int i = 0; i < IterationsCount00; i++) + { + count += products + .GroupBy(p => p.Category) + .ToDictionary(c => c, g => g.Count()) + .Count(); + } + + return (count == 5 * IterationsCountBy00); + } + + [Benchmark] + public bool CountBy00LookupX() + { + List products = Product.GetProductList(); + int count = 0; + for (int i = 0; i < IterationsCount00; i++) + { + count += products + .ToLookup(p => p.Category) + .ToDictionary(c => c, g => g.Count()) + .Count(); + } + + return (count == 5 * IterationsCountBy00); + } + #endregion } From 658582b4831438321435f0acb5457774afb71eaf Mon Sep 17 00:00:00 2001 From: Emmanuel ANDRE <2341261+manandre@users.noreply.github.com> Date: Sat, 7 Oct 2023 22:48:04 +0200 Subject: [PATCH 2/3] Add AggregateBy benchmarks --- src/benchmarks/micro/runtime/Linq/Linq.cs | 100 +++++++++++++++++++++- 1 file changed, 96 insertions(+), 4 deletions(-) diff --git a/src/benchmarks/micro/runtime/Linq/Linq.cs b/src/benchmarks/micro/runtime/Linq/Linq.cs index 8513dff8900..292b4408a2d 100644 --- a/src/benchmarks/micro/runtime/Linq/Linq.cs +++ b/src/benchmarks/micro/runtime/Linq/Linq.cs @@ -119,6 +119,8 @@ public class LinqBenchmarks public const int IterationsCount00 = 1000000; public const int IterationsOrder00 = 25000; public const int IterationsCountBy00 = 1000000; + public const int IterationsAggregateBy00 = 1000000; + public const int IterationsGroupBy00 = 1000000; #region Where00 @@ -366,9 +368,26 @@ public bool CountBy00LinqMethodX() { List products = Product.GetProductList(); int count = 0; - for (int i = 0; i < IterationsCount00; i++) + for (int i = 0; i < IterationsCountBy00; i++) + { + count += products + .CountBy(p => p.Category) + .Count(); + } + + return (count == 5 * IterationsCountBy00); + } + + [Benchmark] + public bool CountBy00AggregateByX() + { + List products = Product.GetProductList(); + int count = 0; + for (int i = 0; i < IterationsCountBy00; i++) { - count += products.CountBy(p => p.Category).Count(); + count += products + .AggregateBy(p => p.Category, 0, (count, _) => ++count) + .Count(); } return (count == 5 * IterationsCountBy00); @@ -380,7 +399,7 @@ public bool CountBy00GroupByX() { List products = Product.GetProductList(); int count = 0; - for (int i = 0; i < IterationsCount00; i++) + for (int i = 0; i < IterationsCountBy00; i++) { count += products .GroupBy(p => p.Category) @@ -396,7 +415,7 @@ public bool CountBy00LookupX() { List products = Product.GetProductList(); int count = 0; - for (int i = 0; i < IterationsCount00; i++) + for (int i = 0; i < IterationsCountBy00; i++) { count += products .ToLookup(p => p.Category) @@ -407,4 +426,77 @@ public bool CountBy00LookupX() return (count == 5 * IterationsCountBy00); } #endregion + + #region AggregateBy00 + +#if NET9_0_OR_GREATER + [Benchmark] + public bool AggregateBy00LinqMethodX() + { + List products = Product.GetProductList(); + int sum = 0; + for (int i = 0; i < IterationsAggregateBy00; i++) + { + sum += products + .AggregateBy(p => p.Category, 0, (total, p) => total + p.UnitsInStock * p.UnitPrice) + .Sum(); + } + + return (sum == 5 * IterationsAggregateBy00); + } + + [Benchmark] + public bool AggregateBy00GroupByX() + { + List products = Product.GetProductList(); + int count = 0; + for (int i = 0; i < IterationsAggregateBy00; i++) + { + count += products + .GroupBy(p => p.Category) + .ToDictionary(c => c, g => g.Aggregate(0, (total, p) => total + p.UnitsInStock * p.UnitPrice) + .Sum(); + } + + return (count == 5 * IterationsAggregateBy00); + } +#endif + + #endregion + + #region GroupBy00 + + [Benchmark] + public bool GroupBy00LinqMethodX() + { + List products = Product.GetProductList(); + int count = 0; + for (int i = 0; i < IterationsGroupBy00; i++) + { + count += products + .GroupBy(p => p.Category) + .Count(); + } + + return (count == 5 * IterationsGroupBy00); + } + +#if NET9_0_OR_GREATER + [Benchmark] + public bool GroupBy00AggregateByX() + { + List products = Product.GetProductList(); + int count = 0; + for (int i = 0; i < IterationsGroupBy00; i++) + { + count += products + .AggregateBy(p => p.Category, _ => new List(), (group, element) => { group.Add(element); return group;}) + .Count(); + } + + return (count == 5 * IterationsGroupBy00); + } +#endif + + #endregion } From 3d80516506001e7ad20bba3e81bfa0c2c6722191 Mon Sep 17 00:00:00 2001 From: Emmanuel ANDRE <2341261+manandre@users.noreply.github.com> Date: Wed, 22 Nov 2023 21:30:19 +0100 Subject: [PATCH 3/3] Rebase and fix benchmarks --- src/benchmarks/micro/runtime/Linq/Linq.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/benchmarks/micro/runtime/Linq/Linq.cs b/src/benchmarks/micro/runtime/Linq/Linq.cs index 292b4408a2d..b47f041e620 100644 --- a/src/benchmarks/micro/runtime/Linq/Linq.cs +++ b/src/benchmarks/micro/runtime/Linq/Linq.cs @@ -434,33 +434,33 @@ public bool CountBy00LookupX() public bool AggregateBy00LinqMethodX() { List products = Product.GetProductList(); - int sum = 0; + decimal sum = 0; for (int i = 0; i < IterationsAggregateBy00; i++) { sum += products - .AggregateBy(p => p.Category, 0, (total, p) => total + p.UnitsInStock * p.UnitPrice) - .Sum(); + .AggregateBy(p => p.Category, decimal.Zero, (total, p) => total + p.UnitsInStock * p.UnitPrice) + .Sum(kvp => kvp.Value); } return (sum == 5 * IterationsAggregateBy00); } +#endif [Benchmark] public bool AggregateBy00GroupByX() { List products = Product.GetProductList(); - int count = 0; + decimal count = 0; for (int i = 0; i < IterationsAggregateBy00; i++) { count += products .GroupBy(p => p.Category) - .ToDictionary(c => c, g => g.Aggregate(0, (total, p) => total + p.UnitsInStock * p.UnitPrice) - .Sum(); + .ToDictionary(c => c, g => g.Aggregate(decimal.Zero, (total, p) => total + p.UnitsInStock * p.UnitPrice)) + .Sum(kvp => kvp.Value); } return (count == 5 * IterationsAggregateBy00); } -#endif #endregion @@ -490,7 +490,7 @@ public bool GroupBy00AggregateByX() for (int i = 0; i < IterationsGroupBy00; i++) { count += products - .AggregateBy(p => p.Category, _ => new List(), (group, element) => { group.Add(element); return group;}) + .AggregateBy(p => p.Category, _ => new List(), (group, element) => { group.Add(element); return group;}) .Count(); }