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.
5 // Integration by romberg method adapted from Conte and de Boor
7 using Microsoft.Xunit.Performance;
9 using System.Runtime.CompilerServices;
12 [assembly: OptimizeForBenchmarks]
14 namespace Benchstone.BenchF
16 public static class Romber
19 public const int Iterations = 1;
21 public const int Iterations = 640000;
24 private static T[][] AllocArray<T>(int n1, int n2)
26 T[][] a = new T[n1][];
27 for (int i = 0; i < n1; ++i)
34 [MethodImpl(MethodImplOptions.NoInlining)]
35 private static bool Bench()
37 double[][] r = AllocArray<double>(11, 11);
38 double[][] t = AllocArray<double>(11, 11);
40 int idbg, m, n, i, kmax, fourj, j, kmaxm2, l, k, mm1;
41 double sum, ratio, t1, h, a, b;
43 for (l = 1; l <= Iterations; l++)
51 sum = (F(a) + F(b)) / 2;
62 for (i = 1; i <= mm1; i++)
72 System.Console.WriteLine(" romberg t-table \n");
73 System.Console.WriteLine("{0}\n", t[1][1]);
76 for (k = 2; k <= kmax; k++)
81 for (i = 1; i <= n / 2; i++)
83 r[k][1] = r[k - 1][1] * System.Math.Sqrt(b * mm1);
88 t[k][1] = t[k - 1][1] / 2 + sum * h;
90 for (j = 2; j <= k; j++)
93 t[k - 1][j - 1] = t[k][j - 1] - t[k - 1][j - 1];
94 t[k][j] = t[k][j - 1] + t[k - 1][j - 1] / (fourj - 1);
100 System.Console.WriteLine("{0} {1} {2}d\n", t[k][j], j, k);
112 System.Console.WriteLine(" table of ratios \n");
115 for (k = 1; k <= kmaxm2; k++)
117 for (j = 1; j <= k; j++)
120 if (System.Math.Abs(t[k + 1][j]) > 0)
122 ratio = t[k][j] / t[k + 1][j];
131 System.Console.WriteLine("{0} {1} {2}\n", t[k][j], j, k);
142 private static double F(double x)
144 return (System.Math.Exp((-(x)) * (x)));
148 public static void Test()
150 foreach (var iteration in Benchmark.Iterations)
152 using (iteration.StartMeasurement())
159 private static bool TestBase()
161 bool result = Bench();
165 public static int Main()
167 bool result = TestBase();
168 return (result ? 100 : -1);