1
0

MinMaxBenchmark.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading;
  7. using BenchmarkDotNet.Attributes;
  8. namespace Benchmarks.System.Interactive
  9. {
  10. #if !NET6_0_OR_GREATER
  11. [MemoryDiagnoser]
  12. public class MinMaxBenchmark
  13. {
  14. [Params(1, 10, 100, 1000, 10000, 100000, 1000000)]
  15. public int N;
  16. private int _store;
  17. private IList<int>? _listStore;
  18. private readonly IComparer<int> _comparer = Comparer<int>.Default;
  19. [Benchmark]
  20. public void Min()
  21. {
  22. Volatile.Write(ref _store, Enumerable.Range(1, N).Min(_comparer));
  23. }
  24. [Benchmark]
  25. public void MinBy()
  26. {
  27. Volatile.Write(ref _listStore, Enumerable.Range(1, N).MinBy(v => -v, _comparer));
  28. }
  29. [Benchmark]
  30. public void Max()
  31. {
  32. Volatile.Write(ref _store, Enumerable.Range(1, N).Max(_comparer));
  33. }
  34. [Benchmark]
  35. public void MaxBy()
  36. {
  37. Volatile.Write(ref _listStore, Enumerable.Range(1, N).MaxBy(v => -v, _comparer));
  38. }
  39. }
  40. #endif
  41. }