Update CoreClr, PgoData to preview1-26004-01, master-20171204-0047, respectively...
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / BenchF / Trap / Trap.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 // Integration by corrected trapezoid rule adapted from Conte and de Boor
6
7 using Microsoft.Xunit.Performance;
8 using System;
9 using System.Runtime.CompilerServices;
10 using Xunit;
11
12 [assembly: OptimizeForBenchmarks]
13
14 namespace Benchstone.BenchF
15 {
16 public static class Trap
17 {
18 #if DEBUG
19     public const int Iterations = 1;
20 #else
21     public const int Iterations = 240000;
22 #endif
23
24     [MethodImpl(MethodImplOptions.NoInlining)]
25     private static bool Bench()
26     {
27         int nm1, idbg;
28         double t2, cortrp, trap, a, b, h;
29         trap = 0.0;
30         cortrp = 0.0;
31
32         idbg = 0;
33         for (int j = 1; j <= Iterations; j++)
34         {
35             a = 0;
36             b = 1;
37             if (idbg != 0)
38             {
39                 System.Console.WriteLine("trapazoid sum    corr.trap sum \n");
40             }
41
42             for (int n = 10; n <= 15; n++)
43             {
44                 h = (b - a) / n;
45                 nm1 = n - 1;
46                 trap = (F(a) + F(b)) / 2;
47                 for (int i = 1; i <= nm1; i++)
48                 {
49                     t2 = a + i * h;
50                     trap = trap + F(t2);
51                 }
52                 trap = trap * h;
53                 cortrp = trap + h * h * (FPrime(a) - FPrime(b)) / 12;
54                 if (idbg != 0)
55                 {
56                     System.Console.WriteLine("{0}, {1}, {2}\n", n, trap, cortrp);
57                 }
58             }
59         }
60
61         return true;
62     }
63
64     private static double F(double x)
65     {
66         return (System.Math.Exp(-(x) * (x)));
67     }
68
69     private static double FPrime(double x)
70     {
71         return ((-2) * (x) * (F(x)));
72     }
73
74     [Benchmark]
75     public static void Test()
76     {
77         foreach (var iteration in Benchmark.Iterations)
78         {
79             using (iteration.StartMeasurement())
80             {
81                 Bench();
82             }
83         }
84     }
85
86     private static bool TestBase()
87     {
88         bool result = Bench();
89         return result;
90     }
91
92     public static int Main()
93     {
94         bool result = TestBase();
95         return (result ? 100 : -1);
96     }
97 }
98 }