3ce1a98f9fb26f0f55cca75d5a4bee9583efe720
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / BenchI / AddArray2 / AddArray2.cs
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.
4 //
5
6 using Microsoft.Xunit.Performance;
7 using System;
8 using System.Runtime.CompilerServices;
9 using Xunit;
10
11 [assembly: OptimizeForBenchmarks]
12
13 namespace Benchstone.BenchI
14 {
15 public static class AddArray2
16 {
17 #if DEBUG
18     public const int Iterations = 1;
19 #else
20     public const int Iterations = 50;
21 #endif
22
23     private const int Dim = 200;
24
25     private static T[][] AllocArray<T>(int n1, int n2)
26     {
27         T[][] a = new T[n1][];
28         for (int i = 0; i < n1; ++i)
29         {
30             a[i] = new T[n2];
31         }
32         return a;
33     }
34
35     private static
36     void BenchInner1(int[][] a, ref int nn)
37     {
38         int n;
39         int l, m;
40         n = nn;
41         for (int i = 1; i <= n; i++)
42         {
43             for (int j = (i + 1); j <= n; j++)
44             {
45                 for (int k = 1; k <= n; k++)
46                 {
47                     l = a[i][k];
48                     m = a[j][k];
49                     unchecked
50                     {
51                         a[j][k] = l + m;
52                     }
53                 }
54             }
55         }
56     }
57
58     private static
59     void BenchInner2(int[][] a, ref int nn)
60     {
61         int n;
62         int l, m;
63         n = nn;
64         for (int i = 1; i <= n; i++)
65         {
66             for (int j = (i + 1); j <= n; j++)
67             {
68                 for (int k = 1; k <= n; k++)
69                 {
70                     l = a[k][i];
71                     m = a[k][j];
72                     unchecked
73                     {
74                         a[k][j] = l + m;
75                     }
76                 }
77             }
78         }
79     }
80
81     [MethodImpl(MethodImplOptions.NoInlining)]
82     private static bool Bench(int[][] a)
83     {
84         int n = Dim;
85         for (int i = 1; i <= n; i++)
86         {
87             for (int j = 1; j <= n; j++)
88             {
89                 a[i][j] = i + j;
90             }
91         }
92
93         BenchInner1(a, ref n);
94         n = Dim;
95         BenchInner2(a, ref n);
96
97         return true;
98     }
99
100     [Benchmark]
101     public static void Test()
102     {
103         int[][] array = AllocArray<int>(Dim + 1, Dim + 1);
104         foreach (var iteration in Benchmark.Iterations)
105         {
106             using (iteration.StartMeasurement())
107             {
108                 for (int i = 1; i <= Iterations; i++)
109                 {
110                     Bench(array);
111                 }
112             }
113         }
114     }
115
116     private static bool TestBase()
117     {
118         int[][] array = AllocArray<int>(Dim + 1, Dim + 1);
119         bool result = true;
120         for (int i = 1; i <= Iterations; i++)
121         {
122             result &= Bench(array);
123         }
124         return result;
125     }
126
127     public static int Main()
128     {
129         bool result = TestBase();
130         return (result ? 100 : -1);
131     }
132 }
133 }