Update CoreClr, PgoData to preview1-26004-01, master-20171204-0047, respectively...
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / BenchI / AddArray / AddArray.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 AddArray
16 {
17
18 #if DEBUG
19     public const int Iterations = 1;
20 #else
21     public const int Iterations = 15000;
22 #endif
23
24     const int Size = 6000;
25
26     public static volatile object VolatileObject;
27
28     [MethodImpl(MethodImplOptions.NoInlining)]
29     static void Escape(object obj) {
30         VolatileObject = obj;
31     }
32
33     [MethodImpl(MethodImplOptions.NoInlining)]
34     static bool Bench() {
35
36         int[] flags1 = new int[Size + 1];
37         int[] flags2 = new int[Size + 1];
38         int[] flags3 = new int[Size + 1];
39         int[] flags4 = new int[Size + 1];
40
41         int j, k, l, m;
42
43         for (j = 0; j <= Size; j++) {
44             flags1[j] = 70000 + j;
45             k = j;
46             flags2[k] = flags1[j] + k + k;
47             l = j;
48             flags3[l] = flags2[k] + l + l + l;
49             m = j;
50             flags4[m] = flags3[l] + m + m + m + m;
51         }
52
53         for (j = 0; j <= Size; j++) {
54             k = j;
55             l = j;
56             m = j;
57             flags1[j] = flags1[j] + flags2[k] + flags3[l] + flags4[m] - flags2[k - j + l];
58         }
59
60         // Escape each flags array so that their elements will appear live-out
61         Escape(flags1);
62         Escape(flags2);
63         Escape(flags3);
64         Escape(flags4);
65
66         return true;
67     }
68
69     [Benchmark]
70     public static void Test() {
71         foreach (var iteration in Benchmark.Iterations) {
72             using (iteration.StartMeasurement()) {
73                 for (int i = 0; i < Iterations; i++) {
74                     Bench();
75                 }
76             }
77         }
78     }
79
80     static bool TestBase() {
81         bool result = true;
82         for (int i = 0; i < Iterations; i++) {
83             result &= Bench();
84         }
85         return result;
86     }
87
88     public static int Main() {
89         bool result = TestBase();
90         return (result ? 100 : -1);
91     }
92 }
93 }