summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Performance/CodeQuality/BenchI/Array1/Array1.cs
blob: 5e383ace66bb2dc21d02367789e2056ffe33a3ec (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// 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 Microsoft.Xunit.Performance;
using System;
using System.Runtime.CompilerServices;
using Xunit;

[assembly: OptimizeForBenchmarks]

namespace Benchstone.BenchI
{
public static class Array1
{
#if DEBUG
    private const int Iterations = 1;
    private const int Maxnum = 100;
#else
    private const int Iterations = 125;
    private const int Maxnum = 1000;
#endif

    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;
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    private static bool Bench()
    {
        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;
    }

    [Benchmark]
    public static void Test()
    {
        foreach (var iteration in Benchmark.Iterations)
        {
            using (iteration.StartMeasurement())
            {
                for (int i = 0; i < Iterations; i++)
                {
                    Bench();
                }
            }
        }
    }

    private static bool TestBase()
    {
        bool result = true;
        for (int i = 0; i < Iterations; i++)
        {
            result &= Bench();
        }
        return result;
    }

    public static int Main()
    {
        bool result = TestBase();
        return (result ? 100 : -1);
    }
}
}