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 // The secant algorithm adapted from Conte and DeBoor
7 using Microsoft.Xunit.Performance;
9 using System.Runtime.CompilerServices;
12 [assembly: OptimizeForBenchmarks]
14 namespace Benchstone.BenchF
16 public static class Secant
19 public const int Iterations = 1;
21 public const int Iterations = 3000000;
24 public static volatile object VolatileObject;
26 [MethodImpl(MethodImplOptions.NoInlining)]
27 private static void Escape(object obj)
32 [MethodImpl(MethodImplOptions.NoInlining)]
33 private static bool Bench()
43 for (int i = 1; i <= Iterations; i++)
47 Inner(ref x0, ref x1, 0.0000001, 0.0000001, 30, out iflag);
56 System.Console.WriteLine(" the root is {0:E}, F(ROOT):= {1:E}\n", x1, fx1);
63 // Escape iflag, x1, and fx1 so that they appear live
71 private static double FF(double x)
73 return (-1.0 - (x * (1.0 - (x * x))));
76 private static void Inner(ref double x0, ref double x1, double xtol, double ftol, int ntol, out int iflag)
78 double deltax, deltaf, f0, f1;
84 for (int n = 1; n <= ntol; n++)
88 if (System.Math.Abs(f1) <= ftol)
99 deltax = f1 / deltaf * deltax;
102 if (System.Math.Abs(deltax) <= xtol)
121 public static void Test()
123 foreach (var iteration in Benchmark.Iterations)
125 using (iteration.StartMeasurement())
132 private static bool TestBase()
134 bool result = Bench();
138 public static int Main()
140 bool result = TestBase();
141 return (result ? 100 : -1);