Update CoreClr, PgoData to preview1-26004-01, master-20171204-0047, respectively...
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / Benchstones / BenchI / BenchE / BenchE.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 BenchE
16 {
17 #if DEBUG
18     public const int Iterations = 1;
19 #else
20     public const int Iterations = 5000000;
21 #endif
22
23     private static int s_position;
24
25     private static int Strsch(char[] s, char[] k, int ns, int nk)
26     {
27         int i, j;
28         int start, ksave, cont;
29         int kend, ssave;
30         int r;
31
32         start = 0;
33         ksave = 0;
34         cont = ns - nk + start;
35         kend = ksave + nk - 1;
36         i = 0;
37         j = 0;
38     top:
39         while (s[i] != k[j])
40         {
41             // s is accessed upto cont i.e. ns - nk + 0
42             if (i >= cont)
43             {
44                 r = -1;
45                 goto bottom;
46             }
47             i = i + 1;
48         }
49         ssave = i;
50         j = j + 1;
51         while (j <= kend)
52         {
53             i = i + 1;
54             // j <= kend, so k is accessed upto 0 + nk - 1
55             if (s[i] != k[j])
56             {
57                 i = ssave + 1;
58                 j = ksave;
59                 goto top;
60             }
61             j = j + 1;
62         }
63         r = ssave - start + 1;
64     bottom:
65         return r;
66     }
67
68     private static void BenchInner(char[] s, char[] k)
69     {
70         int ns, nk;
71
72         ns = 120;
73         nk = 15;
74         s_position = Strsch(s, k, ns, nk);
75     }
76
77     [MethodImpl(MethodImplOptions.NoInlining)]
78     private static bool Bench()
79     {
80         char[] s = {
81             '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', 'H', 'E', 'R', 'E', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
82             'H', 'E', 'R', 'E', ' ', 'I', 'S', ' ', 'A', ' ', 'M', 'A', 'T', 'C', 'H', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'
83         };
84
85         char[] k = { 'H', 'E', 'R', 'E', ' ', 'I', 'S', ' ', 'A', ' ', 'M', 'A', 'T', 'C', 'H', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
86
87         for (int i = 0; i < Iterations; i++)
88         {
89             BenchInner(s, k);
90         }
91
92         return (s_position == 91);
93     }
94
95     [Benchmark]
96     public static void Test()
97     {
98         foreach (var iteration in Benchmark.Iterations)
99         {
100             using (iteration.StartMeasurement())
101             {
102                 Bench();
103             }
104         }
105     }
106
107     private static bool TestBase()
108     {
109         bool result = Bench();
110         return result;
111     }
112
113     public static int Main()
114     {
115         bool result = TestBase();
116         return (result ? 100 : -1);
117     }
118 }
119 }