47fbf3730a2996854361e0ec22e3f59530235287
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / BenchI / XposMatrix / XposMatrix.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 XposMatrix
16 {
17     public const int ArraySize = 100;
18
19 #if DEBUG
20     public const int Iterations = 1;
21 #else
22     public const int Iterations = 25000;
23 #endif
24
25     static T[][] AllocArray<T>(int n1, int n2) {
26         T[][] a = new T[n1][];
27         for (int i = 0; i < n1; ++i) {
28             a[i] = new T[n2];
29         }
30         return a;
31     }
32
33     static void Inner(int[][] x, int n) {
34         for (int i = 1; i <= n; i++) {
35             for (int j = 1; j <= n; j++) {
36                 int t = x[i][j];
37                 x[i][j] = x[j][i];
38                 x[j][i] = t;
39             }
40         }
41     }
42
43     [MethodImpl(MethodImplOptions.NoInlining)]
44     static bool Bench(int[][] matrix) {
45
46         int n = ArraySize;
47         for (int i = 1; i <= n; i++) {
48             for (int j = 1; j <= n; j++) {
49                 matrix[i][j] = 1;
50             }
51         }
52
53         if (matrix[n][n] != 1) {
54             return false;
55         }
56
57         Inner(matrix, n);
58
59         if (matrix[n][n] != 1) {
60             return false;
61         }
62
63         return true;
64     }
65
66     [Benchmark]
67     public static void Test() {
68         int[][] matrix = AllocArray<int>(ArraySize + 1, ArraySize + 1);
69         foreach (var iteration in Benchmark.Iterations) {
70             using (iteration.StartMeasurement()) {
71                 for (int i = 0; i < Iterations; i++) {
72                     Bench(matrix);
73                 }
74             }
75         }
76     }
77
78     static bool TestBase() {
79         int[][] matrix = AllocArray<int>(ArraySize + 1, ArraySize + 1);
80         bool result = true;
81         for (int i = 0; i < Iterations; i++) {
82             result &= Bench(matrix);
83         }
84         return result;
85     }
86
87     public static int Main() {
88         bool result = TestBase();
89         return (result ? 100 : -1);
90     }
91 }
92 }