Created
February 3, 2026 15:34
-
-
Save wcabus/ee268e577e54d8f6408b817d267baa3b to your computer and use it in GitHub Desktop.
.NET 10 deabstraction benchmark sample
This file contains hidden or 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 System.Collections.Generic; | |
| using System.Runtime.CompilerServices; | |
| using BenchmarkDotNet.Attributes; | |
| using BenchmarkDotNet.Jobs; | |
| using BenchmarkDotNet.Order; | |
| using BenchmarkDotNet.Running; | |
| // ReSharper disable ArrangeObjectCreationWhenTypeEvident | |
| /* | |
| To run these benchmarks, you'll need to add some target frameworks in the .csproj file and add the BenchmarkDotNet package reference: | |
| <PropertyGroup> | |
| <OutputType>Exe</OutputType> | |
| <TargetFrameworks>net48;net6.0;net8.0;net9.0;net10.0</TargetFrameworks> | |
| /PropertyGroup> | |
| <ItemGroup> | |
| <PackageReference Include="BenchmarkDotNet" Version="0.15.8" /> | |
| </ItemGroup> | |
| */ | |
| namespace DotNet10Benchmark | |
| { | |
| public class Program | |
| { | |
| public static void Main() | |
| { | |
| var summary = BenchmarkRunner.Run(typeof(Program).Assembly); | |
| } | |
| } | |
| [MemoryDiagnoser] | |
| [SimpleJob(RuntimeMoniker.Net48)] | |
| [SimpleJob(RuntimeMoniker.Net60)] | |
| [SimpleJob(RuntimeMoniker.Net80)] | |
| [SimpleJob(RuntimeMoniker.Net90)] | |
| [SimpleJob(RuntimeMoniker.Net10_0)] | |
| [Orderer(jobOrderPolicy: JobOrderPolicy.Numeric)] | |
| public class EnumerableBenchmark | |
| { | |
| private readonly int[] _array = new int[100]; | |
| private readonly List<int> _list = new List<int>(100); | |
| private readonly Stack<int> _stack = new Stack<int>(100); | |
| private readonly Queue<int> _queue = new Queue<int>(100); | |
| public EnumerableBenchmark() | |
| { | |
| for (var i = 0; i < 100; i++) | |
| { | |
| _array[i] = i; | |
| _list.Add(i); | |
| _stack.Push(i); | |
| _queue.Enqueue(i); | |
| } | |
| } | |
| [Benchmark] | |
| public void EnumerateArray() | |
| { | |
| Enumerate(_array); | |
| } | |
| [Benchmark] | |
| public void EnumerateList() | |
| { | |
| Enumerate(_list); | |
| } | |
| [Benchmark] | |
| public void EnumerateStack() | |
| { | |
| Enumerate(_stack); | |
| } | |
| [Benchmark] | |
| public void EnumerateQueue() | |
| { | |
| Enumerate(_queue); | |
| } | |
| [MethodImpl(MethodImplOptions.NoInlining)] | |
| private int Enumerate(IEnumerable<int> values) | |
| { | |
| var sum = 0; | |
| foreach (var item in values) | |
| { | |
| sum += item; | |
| } | |
| return sum; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment