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 // This program solves the "lorenz" equations using Runge-Kutta 4
7 using Microsoft.Xunit.Performance;
9 using System.Runtime.CompilerServices;
12 [assembly: OptimizeForBenchmarks]
14 namespace Benchstone.BenchF
16 public static class Lorenz
19 public const int Iterations = 1;
21 public const int Iterations = 8000000;
24 private static double s_t = 0.0;
25 private static double s_x = 5.0;
26 private static double s_y = 2.0;
27 private static double s_z = 27.0;
29 private static int s_nsteps = Iterations;
30 private static double s_h = -1.0;
31 private static int s_printDerivative = -1;
33 [MethodImpl(MethodImplOptions.NoInlining)]
34 private static bool Bench()
36 double k1, k2, k3, k4;
37 double l1, l2, l3, l4;
38 double m1, m2, m3, m4;
44 s_h = 20.0 / (double)s_nsteps;
46 if (s_printDerivative < 0)
48 s_printDerivative = s_nsteps;
54 for (i = 0; i < s_nsteps; ++i)
56 double t_arg, x_arg, y_arg, z_arg;
58 k1 = F(s_t, s_x, s_y, s_z);
59 l1 = G(s_t, s_x, s_y, s_z);
60 m1 = H(s_t, s_x, s_y, s_z);
63 x_arg = s_x + hdiv2 * k1;
64 y_arg = s_y + hdiv2 * l1;
65 z_arg = s_z + hdiv2 * m1;
67 k2 = F(t_arg, x_arg, y_arg, z_arg);
68 l2 = G(t_arg, x_arg, y_arg, z_arg);
69 m2 = H(t_arg, x_arg, y_arg, z_arg);
71 x_arg = s_x + hdiv2 * k2;
72 y_arg = s_y + hdiv2 * l2;
73 z_arg = s_z + hdiv2 * m2;
75 k3 = F(t_arg, x_arg, y_arg, z_arg);
76 l3 = G(t_arg, x_arg, y_arg, z_arg);
77 m3 = H(t_arg, x_arg, y_arg, z_arg);
80 x_arg = s_x + s_h * k3;
81 y_arg = s_y + s_h * l3;
82 z_arg = s_z + s_h * m3;
84 k4 = F(t_arg, x_arg, y_arg, z_arg);
85 l4 = G(t_arg, x_arg, y_arg, z_arg);
86 m4 = H(t_arg, x_arg, y_arg, z_arg);
88 s_x = s_x + hdiv6 * (k1 + 2.0 * k2 + 2.0 * k3 + k4);
89 s_y = s_y + hdiv6 * (l1 + 2.0 * l2 + 2.0 * l3 + l4);
90 s_z = s_z + hdiv6 * (m1 + 2.0 * m2 + 2.0 * m3 + m4);
97 private static double F(double t, double x, double y, double z)
99 return (10.0 * (y - x));
102 private static double G(double t, double x, double y, double z)
104 return (x * (28.0 - z) - y);
107 private static double H(double t, double x, double y, double z)
109 return (x * y - (8.0 * z) / 3.0);
113 public static void Test()
115 foreach (var iteration in Benchmark.Iterations)
117 using (iteration.StartMeasurement())
124 private static bool TestBase()
126 bool result = Bench();
130 public static int Main()
132 bool result = TestBase();
133 return (result ? 100 : -1);