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.
5 // The sorting benchmark calls a random number generator the number
6 // of times specified by Maxnum to create an array of int integers,
7 // then does a quicksort on the array of ints. Random numbers
8 // are produced using a multiplicative modulus method with known
9 // seed, so that the generated array is constant across compilers.
11 // This is adapted from a benchmark in BYTE Magazine, August 1984.
13 using Microsoft.Xunit.Performance;
15 using System.Runtime.CompilerServices;
18 [assembly: OptimizeForBenchmarks]
20 namespace Benchstone.BenchI
22 public static class Array1
25 private const int Iterations = 1;
26 private const int Maxnum = 100;
28 private const int Iterations = 125;
29 private const int Maxnum = 1000;
32 private const int Modulus = ((int)0x20000);
33 private const int C = 13849;
34 private const int A = 25173;
35 static int s_seed = 7;
37 private static void Quick(int lo, int hi, int[] input)
45 for (i = lo, j = (hi + 1), pivot = input[lo]; ;)
50 } while (input[i] < pivot);
56 } while (input[j] > pivot);
72 Quick(lo, j - 1, input);
73 Quick(j + 1, hi, input);
77 private static int Random(int size)
81 s_seed = s_seed * A + C;
84 return (s_seed % size);
87 private static bool VerifySort(int[] buffer)
89 for (int y = 0; y < Maxnum - 2; y++)
91 if (buffer[y] > buffer[y + 1])
99 [MethodImpl(MethodImplOptions.NoInlining)]
100 private static bool Bench()
102 int[] buffer = new int[Maxnum + 1];
104 for (int i = 0; i < Iterations; ++i)
106 for (int j = 0; j < Maxnum; ++j)
108 int temp = Random(Modulus);
115 buffer[Maxnum] = Modulus;
117 Quick(0, Maxnum - 1, buffer);
120 bool result = VerifySort(buffer);
126 public static void Test()
128 foreach (var iteration in Benchmark.Iterations)
130 using (iteration.StartMeasurement())
132 for (int i = 0; i < Iterations; i++)
140 private static bool TestBase()
143 for (int i = 0; i < Iterations; i++)
150 public static int Main()
152 bool result = TestBase();
153 return (result ? 100 : -1);