Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CountBy and AggregateBy benchmarks #3348

Merged
merged 3 commits into from
Nov 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 144 additions & 1 deletion src/benchmarks/micro/runtime/Linq/Linq.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ public class LinqBenchmarks
public const int IterationsWhere01 = 250000;
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

[Benchmark]
Expand Down Expand Up @@ -356,4 +359,144 @@ public bool Order00ManualX()
return (medianPricedProduct.ProductID == 57);
}
#endregion

#region CountBy00

#if NET9_0_OR_GREATER
[Benchmark]
public bool CountBy00LinqMethodX()
{
List<Product> products = Product.GetProductList();
int count = 0;
for (int i = 0; i < IterationsCountBy00; i++)
{
count += products
.CountBy(p => p.Category)
.Count();
}

return (count == 5 * IterationsCountBy00);
}

[Benchmark]
public bool CountBy00AggregateByX()
{
List<Product> products = Product.GetProductList();
int count = 0;
for (int i = 0; i < IterationsCountBy00; i++)
{
count += products
.AggregateBy(p => p.Category, 0, (count, _) => ++count)
.Count();
}

return (count == 5 * IterationsCountBy00);
}
#endif

[Benchmark]
public bool CountBy00GroupByX()
{
List<Product> products = Product.GetProductList();
int count = 0;
for (int i = 0; i < IterationsCountBy00; i++)
{
count += products
.GroupBy(p => p.Category)
.ToDictionary(c => c, g => g.Count())
.Count();
}

return (count == 5 * IterationsCountBy00);
}

[Benchmark]
public bool CountBy00LookupX()
{
List<Product> products = Product.GetProductList();
int count = 0;
for (int i = 0; i < IterationsCountBy00; i++)
{
count += products
.ToLookup(p => p.Category)
.ToDictionary(c => c, g => g.Count())
.Count();
}

return (count == 5 * IterationsCountBy00);
}
#endregion

#region AggregateBy00

#if NET9_0_OR_GREATER
[Benchmark]
public bool AggregateBy00LinqMethodX()
{
List<Product> products = Product.GetProductList();
decimal sum = 0;
for (int i = 0; i < IterationsAggregateBy00; i++)
{
sum += products
.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<Product> products = Product.GetProductList();
decimal count = 0;
for (int i = 0; i < IterationsAggregateBy00; i++)
{
count += products
.GroupBy(p => p.Category)
.ToDictionary(c => c, g => g.Aggregate(decimal.Zero, (total, p) => total + p.UnitsInStock * p.UnitPrice))
.Sum(kvp => kvp.Value);
}

return (count == 5 * IterationsAggregateBy00);
}

#endregion

#region GroupBy00

[Benchmark]
public bool GroupBy00LinqMethodX()
{
List<Product> 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<Product> products = Product.GetProductList();
int count = 0;
for (int i = 0; i < IterationsGroupBy00; i++)
{
count += products
.AggregateBy(p => p.Category, _ => new List<Product>(), (group, element) => { group.Add(element); return group;})
.Count();
}

return (count == 5 * IterationsGroupBy00);
}
#endif

#endregion
}
Loading