-
Notifications
You must be signed in to change notification settings - Fork 272
/
Array1.cs
117 lines (102 loc) · 2.93 KB
/
Array1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//
// The sorting benchmark calls a random number generator the number
// of times specified by Maxnum to create an array of int integers,
// then does a quicksort on the array of ints. Random numbers
// are produced using a multiplicative modulus method with known
// seed, so that the generated array is constant across compilers.
//
// This is adapted from a benchmark in BYTE Magazine, August 1984.
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
namespace Benchstone.BenchI
{
[BenchmarkCategory(Categories.Runtime, Categories.Benchstones, Categories.JIT, Categories.BenchI)]
public class Array1
{
private const int Iterations = 125;
private const int Maxnum = 1000;
private const int Modulus = ((int)0x20000);
private const int C = 13849;
private const int A = 25173;
static int s_seed = 7;
private static void Quick(int lo, int hi, int[] input)
{
int i, j;
int pivot, temp;
if (lo < hi)
{
// 0 <= lo < hi
for (i = lo, j = (hi + 1), pivot = input[lo]; ;)
{
do
{
++i;
} while (input[i] < pivot);
do
{
--j;
// Accessing upto hi
} while (input[j] > pivot);
if (i < j)
{
temp = input[i];
input[i] = input[j];
input[j] = temp;
}
else
{
break;
}
}
temp = input[j];
input[j] = input[lo];
input[lo] = temp;
Quick(lo, j - 1, input);
Quick(j + 1, hi, input);
}
}
private static int Random(int size)
{
unchecked
{
s_seed = s_seed * A + C;
}
return (s_seed % size);
}
private static bool VerifySort(int[] buffer)
{
for (int y = 0; y < Maxnum - 2; y++)
{
if (buffer[y] > buffer[y + 1])
{
return false;
}
}
return true;
}
[Benchmark(Description = nameof(Array1))]
public bool Test()
{
int[] buffer = new int[Maxnum + 1];
for (int i = 0; i < Iterations; ++i)
{
for (int j = 0; j < Maxnum; ++j)
{
int temp = Random(Modulus);
if (temp < 0L)
{
temp = (-temp);
}
buffer[j] = temp;
}
buffer[Maxnum] = Modulus;
Quick(0, Maxnum - 1, buffer);
}
bool result = VerifySort(buffer);
return result;
}
}
}