KEMBAR78
Make Enumerable.First/Last/GetElement faster on various enumerables by stephentoub · Pull Request #97905 · dotnet/runtime · GitHub
Skip to content

Conversation

@stephentoub
Copy link
Member

Implement IPartition on more (existing) custom iterator types, in particular on the types for Where.Select, Concat, and Reverse.

Method Toolchain Mean Ratio Allocated Alloc Ratio
WhereSelect_First \main\corerun.exe 29.161 ns 1.00 104 B 1.00
WhereSelect_First \pr\corerun.exe 13.829 ns 0.47 40 B 0.38
WhereSelect_Last \main\corerun.exe 2,815.028 ns 1.00 104 B 1.00
WhereSelect_Last \pr\corerun.exe 1,361.385 ns 0.48 40 B 0.38
ArrayReverse_First \main\corerun.exe 440.176 ns 1.00 4072 B 1.00
ArrayReverse_First \pr\corerun.exe 8.615 ns 0.02 - 0.00
ArrayReverse_Last \main\corerun.exe 1,991.332 ns 1.000 4072 B 1.00
ArrayReverse_Last \pr\corerun.exe 8.688 ns 0.004 - 0.00
ConcatComplicated_First \main\corerun.exe 46.294 ns 1.00 168 B 1.00
ConcatComplicated_First \pr\corerun.exe 23.029 ns 0.50 40 B 0.24
ConcatComplicated_Last \main\corerun.exe 22,536.189 ns 1.000 8416 B 1.00
ConcatComplicated_Last \pr\corerun.exe 18.410 ns 0.001 - 0.00
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

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

[MemoryDiagnoser(false)]
public class Tests
{
    private IEnumerable<int> _whereSelect;
    private IEnumerable<int> _arrayReverse;
    private IEnumerable<int> _concatComplicated;

    [GlobalSetup]
    public void Setup()
    {
        _whereSelect = Enumerable.Range(0, 1000).Where(i => i % 2 == 0).Select(i => i * 2);
        _arrayReverse = Enumerable.Range(0, 1000).ToArray().Reverse();
        _concatComplicated = _whereSelect.Concat(_arrayReverse).Concat(_whereSelect).Concat(_arrayReverse);
    }

    [Benchmark] public int WhereSelect_First() => _whereSelect.First();
    [Benchmark] public int WhereSelect_Last() => _whereSelect.Last();

    [Benchmark] public int ArrayReverse_First() => _arrayReverse.First();
    [Benchmark] public int ArrayReverse_Last() => _arrayReverse.Last();

    [Benchmark] public int ConcatComplicated_First() => _concatComplicated.First();
    [Benchmark] public int ConcatComplicated_Last() => _concatComplicated.Last();
}

Implement IPartition on more (existing) custom iterator types, in particular on the types for Where.Select, Concat, and Reverse.
@ghost
Copy link

ghost commented Feb 2, 2024

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

Issue Details

Implement IPartition on more (existing) custom iterator types, in particular on the types for Where.Select, Concat, and Reverse.

Method Toolchain Mean Ratio Allocated Alloc Ratio
WhereSelect_First \main\corerun.exe 29.161 ns 1.00 104 B 1.00
WhereSelect_First \pr\corerun.exe 13.829 ns 0.47 40 B 0.38
WhereSelect_Last \main\corerun.exe 2,815.028 ns 1.00 104 B 1.00
WhereSelect_Last \pr\corerun.exe 1,361.385 ns 0.48 40 B 0.38
ArrayReverse_First \main\corerun.exe 440.176 ns 1.00 4072 B 1.00
ArrayReverse_First \pr\corerun.exe 8.615 ns 0.02 - 0.00
ArrayReverse_Last \main\corerun.exe 1,991.332 ns 1.000 4072 B 1.00
ArrayReverse_Last \pr\corerun.exe 8.688 ns 0.004 - 0.00
ConcatComplicated_First \main\corerun.exe 46.294 ns 1.00 168 B 1.00
ConcatComplicated_First \pr\corerun.exe 23.029 ns 0.50 40 B 0.24
ConcatComplicated_Last \main\corerun.exe 22,536.189 ns 1.000 8416 B 1.00
ConcatComplicated_Last \pr\corerun.exe 18.410 ns 0.001 - 0.00
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

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

[MemoryDiagnoser(false)]
public class Tests
{
    private IEnumerable<int> _whereSelect;
    private IEnumerable<int> _arrayReverse;
    private IEnumerable<int> _concatComplicated;

    [GlobalSetup]
    public void Setup()
    {
        _whereSelect = Enumerable.Range(0, 1000).Where(i => i % 2 == 0).Select(i => i * 2);
        _arrayReverse = Enumerable.Range(0, 1000).ToArray().Reverse();
        _concatComplicated = _whereSelect.Concat(_arrayReverse).Concat(_whereSelect).Concat(_arrayReverse);
    }

    [Benchmark] public int WhereSelect_First() => _whereSelect.First();
    [Benchmark] public int WhereSelect_Last() => _whereSelect.Last();

    [Benchmark] public int ArrayReverse_First() => _arrayReverse.First();
    [Benchmark] public int ArrayReverse_Last() => _arrayReverse.Last();

    [Benchmark] public int ConcatComplicated_First() => _concatComplicated.First();
    [Benchmark] public int ConcatComplicated_Last() => _concatComplicated.Last();
}
Author: stephentoub
Assignees: -
Labels:

area-System.Linq

Milestone: -

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants