Created
February 12, 2025 12:30
-
-
Save davepcallan/3b93132b187ed6b2de304bdf4c687926 to your computer and use it in GitHub Desktop.
.NET Framework 4.8 v .NET 9 LINQ benchmarks
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.Jobs; | |
using BenchmarkDotNet.Reports; | |
using BenchmarkDotNet.Running; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Benchmarks | |
{ | |
[MemoryDiagnoser] | |
[Config(typeof(Config))] | |
[SimpleJob(RuntimeMoniker.Net48, baseline: true)] | |
[SimpleJob(RuntimeMoniker.Net90)] | |
[HideColumns(Column.Job, Column.RatioSD, Column.AllocRatio)] | |
[Orderer(BenchmarkDotNet.Order.SummaryOrderPolicy.SlowestToFastest)] | |
public class LINQBenchmarks | |
{ | |
private class Config : ManualConfig | |
{ | |
public Config() | |
{ | |
SummaryStyle = | |
SummaryStyle.Default.WithRatioStyle(RatioStyle.Trend); | |
} | |
} | |
[Params(50)] | |
public int Length { get; set; } | |
private IEnumerable<int> _source, _sourceToCompare; | |
[GlobalSetup] | |
public void Setup() | |
{ | |
_source = Enumerable.Range(1, Length); | |
_sourceToCompare = Enumerable.Range(1, Length); | |
} | |
[Benchmark] | |
public int Min() => _source.Min(); | |
[Benchmark] | |
public int Max() => _source.Max(); | |
[Benchmark] | |
public float Sum() => _source.Sum(); | |
[Benchmark] | |
public double Average() => _source.Average(); | |
[Benchmark] | |
public int Count() => _source.Count(); | |
[Benchmark] | |
public int ElementAt() => _source.ElementAt(5); | |
[Benchmark] | |
public bool SequenceEqual() => | |
_source.SequenceEqual(_sourceToCompare); | |
} | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
BenchmarkRunner.Run<LINQBenchmarks>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment