ff878d612720f54bc5a9a4594b9b3ca1b732f7ee
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / BenchI / IniArray / IniArray.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 IniArray
16 {
17
18 #if DEBUG
19     public const int Iterations = 1;
20 #else
21     public const int Iterations = 10000000;
22 #endif
23
24     const int Allotted = 16;
25     static volatile object VolatileObject;
26
27     static void Escape(object obj) {
28         VolatileObject = obj;
29     }
30
31     [MethodImpl(MethodImplOptions.NoInlining)]
32     static bool Bench() {
33         char[] workarea = new char[Allotted];
34         for (int i = 0; i < Iterations; i++) {
35             for (int j = 0; j < Allotted; j++) {
36                 workarea[j] = ' ';
37             }
38         }
39         Escape(workarea);
40         return true;
41     }
42
43     [Benchmark]
44     public static void Test() {
45         foreach (var iteration in Benchmark.Iterations) {
46             using (iteration.StartMeasurement()) {
47                 Bench();
48             }
49         }
50     }
51
52     static bool TestBase() {
53         bool result = Bench();
54         return result;
55     }
56
57     public static int Main() {
58         bool result = TestBase();
59         return (result ? 100 : -1);
60     }
61 }
62 }