Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Enumerable.Min/Max<T> for value types with Comparer<T>.Default intrinsic #48273

Merged
merged 1 commit into from
Feb 14, 2021

Conversation

stephentoub
Copy link
Member

Method Toolchain Count Mean Ratio
Max \master\corerun.exe 10 70.12 ns 1.00
Max \pr\corerun.exe 10 56.19 ns 0.80
Min \master\corerun.exe 10 67.45 ns 1.00
Min \pr\corerun.exe 10 54.85 ns 0.81
Max \master\corerun.exe 1000000 6,007,980.36 ns 1.00
Max \pr\corerun.exe 1000000 4,666,923.86 ns 0.78
Min \master\corerun.exe 1000000 5,492,475.28 ns 1.00
Min \pr\corerun.exe 1000000 4,401,151.26 ns 0.80
[Params(10, 1_000_000)]
public int Count { get; set; }

[GlobalSetup]
public void Setup()
{
    var r = new Random();
    _times = Enumerable.Range(0, Count).Select(_ => TimeSpan.FromMilliseconds(r.Next(0, int.MaxValue))).ToArray();
}

private TimeSpan[] _times;

[Benchmark]
public TimeSpan Max() => _times.Max();
[Benchmark]
public TimeSpan Min() => _times.Min();

@ghost
Copy link

ghost commented Feb 14, 2021

Tagging subscribers to this area: @eiriktsarpalis
See info in area-owners.md if you want to be subscribed.

Issue Details
Method Toolchain Count Mean Ratio
Max \master\corerun.exe 10 70.12 ns 1.00
Max \pr\corerun.exe 10 56.19 ns 0.80
Min \master\corerun.exe 10 67.45 ns 1.00
Min \pr\corerun.exe 10 54.85 ns 0.81
Max \master\corerun.exe 1000000 6,007,980.36 ns 1.00
Max \pr\corerun.exe 1000000 4,666,923.86 ns 0.78
Min \master\corerun.exe 1000000 5,492,475.28 ns 1.00
Min \pr\corerun.exe 1000000 4,401,151.26 ns 0.80
[Params(10, 1_000_000)]
public int Count { get; set; }

[GlobalSetup]
public void Setup()
{
    var r = new Random();
    _times = Enumerable.Range(0, Count).Select(_ => TimeSpan.FromMilliseconds(r.Next(0, int.MaxValue))).ToArray();
}

private TimeSpan[] _times;

[Benchmark]
public TimeSpan Max() => _times.Max();
[Benchmark]
public TimeSpan Min() => _times.Min();
Author: stephentoub
Assignees: -
Labels:

area-System.Linq

Milestone: -

@stephentoub stephentoub added this to the 6.0.0 milestone Feb 14, 2021
@stephentoub stephentoub added the tenet-performance Performance related issue label Feb 14, 2021
@stephentoub
Copy link
Member Author

Thanks, @EgorBo ;-)

@mburbea
Copy link

mburbea commented Feb 14, 2021

What about optimizing the case for TResult Max(IEnumerable<TSource>, Func<TSource, TResult>)?

@stephentoub
Copy link
Member Author

What about optimizing the case for TResult Max(IEnumerable<TSource>, Func<TSource, TResult>)?

#48289

@@ -465,6 +464,7 @@ public static decimal Max(this IEnumerable<decimal> source)
}
while (value == null);

Comparer<TSource> comparer = Comparer<TSource>.Default;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, what is the motivation for not optimizing reference types?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See issue #10050 for more details

@ghost ghost locked as resolved and limited conversation to collaborators Mar 17, 2021
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.

5 participants