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 QuickSort
19 public const int Iterations = 1;
21 public const int Iterations = 80000;
24 const int MAXNUM = 200;
25 const int MODULUS = 0x20000;
28 static int s_seed = 7;
30 static int Random(int size) {
32 s_seed = s_seed * A + C;
34 return (s_seed % size);
37 static void Quick(int lo, int hi, int[] arr) {
43 for (i = lo, j = hi, pivot = arr[hi]; i < j;) {
44 while (i < j && arr[i] <= pivot){
47 while (j > i && arr[j] >= pivot) {
57 // need to swap the pivot and a[i](or a[j] as i==j) so
58 // that the pivot will be at its final place in the sorted array
65 Quick(lo, i - 1, arr);
66 Quick(i + 1, hi, arr);
70 [MethodImpl(MethodImplOptions.NoInlining)]
73 int[] buffer = new int[MAXNUM];
75 for (int j = 0; j < MAXNUM; ++j) {
76 int temp = Random(MODULUS);
83 Quick(0, MAXNUM - 1, buffer);
85 for (int j = 0; j < MAXNUM - 1; ++j) {
86 if (buffer[j] > buffer[j+1]) {
95 public static void Test() {
96 foreach (var iteration in Benchmark.Iterations) {
97 using (iteration.StartMeasurement()) {
98 for (int i = 0; i < Iterations; i++) {
105 static bool TestBase() {
107 for (int i = 0; i < Iterations; i++) {
113 public static int Main() {
114 bool result = TestBase();
115 return (result ? 100 : -1);