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.
6 using Microsoft.Xunit.Performance;
8 using System.Runtime.CompilerServices;
11 [assembly: OptimizeForBenchmarks]
13 namespace Benchstone.BenchI
15 public static class HeapSort
19 public const int Iterations = 1;
21 public const int Iterations = 2500;
24 const int ArraySize = 5500;
26 static void Inner(int[] x, int n) {
29 // pass1 -- put vector in heap form
30 // that is to say, guarantee that x(i)>=x(2*i) and x(i)>=x(2*i+1).
31 // after pass 1, the largest item will be at x(1).
32 for (i = 2; i <= n; i++) {
50 // pass 2 -- swap first and last items. now with the last
51 // item correctly placed, consider the list shorter by one item.
52 // restore the shortened list to heap sort, and repeat
53 // process until list is only two items long.
56 // do i = n to 2 by -1;
58 x[i] = x[1]; // last item, i.e. item(i) now correct.
59 j = 1; // we now find the appropriate resting point for m
62 // 2 <= k < i ==> 2 <= k < n
66 if (x[k + 1] > x[k]) {
84 [MethodImpl(MethodImplOptions.NoInlining)]
86 int[] x = new int[ArraySize + 1];
87 for (int i = 1; i <= ArraySize; i++) {
88 x[i] = ArraySize - i + 1;
91 for (int j = 1; j <= ArraySize; j++) {
100 public static void Test() {
101 foreach (var iteration in Benchmark.Iterations) {
102 using (iteration.StartMeasurement()) {
103 for (int i = 0; i < Iterations; i++) {
110 static bool TestBase() {
112 for (int i = 0; i < Iterations; i++) {
118 public static int Main() {
119 bool result = TestBase();
120 return (result ? 100 : -1);