Created
October 30, 2024 09:33
-
-
Save sonnemaf/bc0f201bfa6fabad4847359822986af4 to your computer and use it in GitHub Desktop.
Benchmark LINQ First(…) vs Where(…).First() vs Find() in using .NET9, .NET8 and .NET4.8
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.Jobs; | |
using BenchmarkDotNet.Running; | |
BenchmarkRunner.Run<BM>(); | |
[HideColumns("Job", "StdDev", "RatioSD", "Alloc Ratio")] | |
[MemoryDiagnoser(displayGenColumns: false)] | |
[SimpleJob(RuntimeMoniker.Net90)] | |
[SimpleJob(RuntimeMoniker.Net80)] | |
[SimpleJob(RuntimeMoniker.Net48)] | |
public class BM { | |
private readonly List<Employee> _employees = | |
Enumerable.Range(0, 1024).Select(i => new Employee("Fons", i)).ToList(); | |
[Benchmark(Baseline = true)] | |
public Employee? ListFind() => | |
_employees.Find(emp => emp.Salary > 500); | |
[Benchmark] | |
public Employee? LinqFirstOrDefault() => | |
_employees.FirstOrDefault(emp => emp.Salary > 500); | |
[Benchmark] | |
public Employee? LinqWhereFirstOrDefault() | |
=> _employees.Where(emp => emp.Salary > 500).FirstOrDefault(); | |
} | |
public record class Employee(string Name, decimal Salary); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don't forget to update your project file. You need Polysharp to use the record class in NET4.8