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 // Newton's 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 NewtR
19 public const int Iterations = 1;
21 public const int Iterations = 80000000;
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++)
45 Inner(ref x0, 0.0000001, 0.0000001, 10, out iflag);
54 System.Console.WriteLine(" THE ROOT IS {0:e} F(ROOT) := {1:E}\n", x0, fx0);
62 // Escape iflag, x0, and fx0 so that they appear live
70 private static double FF(double x)
72 return (-1.0 - ((x) * (1.0 - ((x) * (x)))));
75 private static double FFDer(double x)
77 return (3.0 * (x) * (x) - 1.0);
80 private static void Inner(ref double x0, double xtol, double ftol, int ntol, out int iflag)
82 double fx0, deriv, deltax;
85 for (int n = 1; n <= ntol; n++)
88 if (System.Math.Abs(fx0) < ftol)
100 if (System.Math.Abs(deltax) < xtol)
110 public static void Test()
112 foreach (var iteration in Benchmark.Iterations)
114 using (iteration.StartMeasurement())
121 private static bool TestBase()
123 bool result = Bench();
127 public static int Main()
129 bool result = TestBase();
130 return (result ? 100 : -1);