Skip to content

Instantly share code, notes, and snippets.

@davepcallan
Last active February 8, 2024 19:05
Show Gist options
  • Save davepcallan/70c4ffbfa6be40d32c5fbf3e9993c3bd to your computer and use it in GitHub Desktop.
Save davepcallan/70c4ffbfa6be40d32c5fbf3e9993c3bd to your computer and use it in GitHub Desktop.
LINQ 6 New features v .NET 7
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<ImplicitUsings>disable</ImplicitUsings>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.3.2064" />
</ItemGroup>
</Project>
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Samples;
internal class Program
{
private static void Main(string[] args)
{
BenchmarkRunner.Run<NewInDotnet6_LINQ_Benchmarks>();
}
}
namespace BenchmarkDotNet.Samples
{
[Config(typeof(Config))]
[SimpleJob(RuntimeMoniker.Net60, baseline:true)]
[SimpleJob(RuntimeMoniker.Net70)]
[SimpleJob(RuntimeMoniker.Net80)]
[MemoryDiagnoser]
[HideColumns(Column.Job, Column.RatioSD, Column.AllocRatio)]
public class NewInDotnet6_LINQ_Benchmarks
{
//Movies taken from https://canro91.github.io/2022/06/27/NET6LinqMethods/
private List<Movie> movies = new List<Movie>
{
new Movie("Titanic", 1998, 4.5f),
new Movie("The Fifth Element", 1997, 4.6f),
new Movie("Terminator 2", 1991, 4.7f),
new Movie("Avatar", 2009, 5),
new Movie("Platoon", 1986, 4),
new Movie("My Neighbor Totoro", 1988, 5)
};
[Benchmark]
public List<Movie> DistinctBy() =>
movies.DistinctBy(movie => movie.Rating).ToList();
[Benchmark]
public List<Movie[]> Chunk() =>
movies.Where(movie => movie.Rating > 4.5f).Chunk(3).ToList();
[Benchmark]
public List<Movie> TakeWithRanges() =>
movies.Where(movie => movie.Rating > 4.5f).Take(^5..3).ToList();
private class Config : ManualConfig
{
public Config()
{
SummaryStyle =
SummaryStyle.Default.WithRatioStyle(RatioStyle.Percentage);
}
}
}
public class Movie
{
public string Title { get; set; }
public int Year { get; set; }
public float Rating { get; set; }
public Movie(string title, int year, float rating)
{
Title = title;
Year = year;
Rating = rating;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment