-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Conversation
stephentoub
commented
Feb 14, 2021
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 |
Tagging subscribers to this area: @eiriktsarpalis Issue Details
[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();
|
Thanks, @EgorBo ;-) |
What about optimizing the case for |
|
@@ -465,6 +464,7 @@ public static decimal Max(this IEnumerable<decimal> source) | |||
} | |||
while (value == null); | |||
|
|||
Comparer<TSource> comparer = Comparer<TSource>.Default; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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