Last active
July 5, 2023 08:49
-
-
Save VaclavElias/b3b521205415d6ae1cf3d5ca9d9cad10 to your computer and use it in GitHub Desktop.
List - ConvertAll vs Select Benchmark
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; | |
namespace Benchmark.Tests | |
{ | |
[MemoryDiagnoser] | |
public class ConvertAllvsSelectBenchmark | |
{ | |
[Params(10, 100)] | |
public int Iterations { get; set; } | |
public List<Apple> Items { get; set; } = new(); | |
[GlobalSetup] | |
public void GlobalSetup() | |
{ | |
Items.AddRange(Enumerable.Range(0, Iterations).Select(_ => new Apple())); | |
} | |
[Benchmark] | |
public void ConvertAllTest() | |
{ | |
var items = Items.ConvertAll(s => s.Color); | |
} | |
[Benchmark] | |
public void SelectTest() | |
{ | |
var items = Items.Select(s => s.Color).ToList(); | |
} | |
} | |
public class Apple | |
{ | |
public string Color { get; set; } = "Red"; | |
public string Origin { get; set; } = "Not Specified"; | |
public void ProcessCost() { } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test on .NET 6
.NET SDK=6.0.100-rc.1.21463.6
[Host] : .NET 6.0.0 (6.0.21.45113), X64 RyuJIT
DefaultJob : .NET 6.0.0 (6.0.21.45113), X64 RyuJIT