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