Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

stephentoub
Copy link
Member

I was recently looking at a codebase that was sorting enums with LINQ and realized we weren't including them as types that are known to not require extra work to maintain stability.

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Tests).Assembly).Run(args);

[MemoryDiagnoser(false)]
[HideColumns("Job", "Error", "StdDev", "Median", "RatioSD")]
public class Tests
{
    private IEnumerable<DayOfWeek> _days = new Random(42).GetItems(Enum.GetValues<DayOfWeek>(), 10);

    [Benchmark]
    public int Order()
    {
        int sum = 0;
        foreach (DayOfWeek dow in _days.Order())
        {
            sum += (int)dow;
        }
        return sum;
    }
}
Method Toolchain Mean Ratio Allocated Alloc Ratio
Order \main\corerun.exe 159.57 ns 1.00 336 B 1.00
Order \pr\corerun.exe 97.77 ns 0.62 184 B 0.55

@stephentoub stephentoub added area-System.Linq tenet-performance Performance related issue labels Mar 11, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-linq
See info in area-owners.md if you want to be subscribed.

@stephentoub stephentoub merged commit 28978b0 into dotnet:main Mar 12, 2024
@stephentoub stephentoub deleted the enumorder branch March 12, 2024 01:52
@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Linq tenet-performance Performance related issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants