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.
6 using Microsoft.Xunit.Performance;
8 using System.Runtime.CompilerServices;
11 [assembly: OptimizeForBenchmarks]
13 namespace Benchstone.BenchF
15 public static class InProd
18 public const int Iterations = 1;
20 public const int Iterations = 70;
23 private const int RowSize = 10 * Iterations;
25 private static int s_seed;
27 private static T[][] AllocArray<T>(int n1, int n2)
29 T[][] a = new T[n1][];
30 for (int i = 0; i < n1; ++i)
37 [MethodImpl(MethodImplOptions.NoInlining)]
38 private static bool Bench()
40 double[][] rma = AllocArray<double>(RowSize, RowSize);
41 double[][] rmb = AllocArray<double>(RowSize, RowSize);
42 double[][] rmr = AllocArray<double>(RowSize, RowSize);
48 for (int i = 1; i < RowSize; i++)
50 for (int j = 1; j < RowSize; j++)
53 for (int k = 1; k < RowSize; k++)
55 sum = sum + rma[i][k] * rmb[k][j];
67 private static void InitRand()
72 private static int Rand()
74 s_seed = (s_seed * 77 + 13218009) % 3687091;
78 private static void InitMatrix(double[][] m)
80 for (int i = 1; i < RowSize; i++)
82 for (int j = 1; j < RowSize; j++)
84 m[i][j] = (Rand() % 120 - 60) / 3;
89 private static void InnerProduct(out double result, double[][] a, double[][] b, int row, int col)
92 for (int i = 1; i < RowSize; i++)
94 result = result + a[row][i] * b[i][col];
98 private static void Inner(double[][] rma, double[][] rmb, double[][] rmr)
103 for (int i = 1; i < RowSize; i++)
105 for (int j = 1; j < RowSize; j++)
107 InnerProduct(out rmr[i][j], rma, rmb, i, j);
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);