Created
January 29, 2024 19:43
-
-
Save davepcallan/e98d3303827004f0f0a2912d6ff6e740 to your computer and use it in GitHub Desktop.
Benchmarks for .NET 9 new LINQ CountBy() method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Columns; | |
using BenchmarkDotNet.Configs; | |
using BenchmarkDotNet.Environments; | |
using BenchmarkDotNet.Jobs; | |
using BenchmarkDotNet.Reports; | |
namespace Benchmarks | |
{ | |
[Config(typeof(Config))] | |
[HideColumns(Column.Job, Column.RatioSD, Column.AllocRatio)] | |
[MemoryDiagnoser] | |
[ReturnValueValidator(true)] | |
public class NewLinq | |
{ | |
private static int SequenceLength = 100000; | |
private static int GroupsCount = 100; | |
private static Random random = new(42); | |
private static readonly int[] Seq = Enumerable | |
.Range(0, SequenceLength) | |
.Select(_ => random.Next(0, GroupsCount)).ToArray(); | |
[Benchmark] | |
public IDictionary<int, int> Grouping() => | |
Seq | |
.GroupBy(x => x) | |
.ToDictionary(x => x.Key, x => x.Count()); | |
[Benchmark] | |
public IDictionary<int, int> LookupDict() => | |
Seq | |
.ToLookup(x => x) | |
.ToDictionary(x => x.Key, x => x.Count()); | |
[Benchmark(Baseline = true)] | |
public IDictionary<int, int> Counting() => | |
Seq | |
.CountBy(x => x) | |
.ToDictionary(x => x.Key, x => x.Value); | |
private class Config : ManualConfig | |
{ | |
public Config() | |
{ | |
AddJob(Job.Default.WithId(".NET 9").WithRuntime(CoreRuntime.Core90)); | |
SummaryStyle = | |
SummaryStyle.Default.WithRatioStyle(RatioStyle.Percentage); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment