Skip to content

Instantly share code, notes, and snippets.

@idavis
Last active January 9, 2018 16:04
Show Gist options
  • Save idavis/4dd2bad1252427400cf05fc3a1a33e9c to your computer and use it in GitHub Desktop.
Save idavis/4dd2bad1252427400cf05fc3a1a33e9c to your computer and use it in GitHub Desktop.
Matrix Multiplication Benchmarks
namespace MatrixMultiplication
{
public static class Jagged
{
public static double[][] Multiply( int N )
{
var C = new double[N][];
var A = new double[N][];
var B = new double[N][];
Util.Initialize( N, A, B, C );
for ( int i = 0; i < N; i++ )
{
for ( int k = 0; k < N; k++ )
{
for ( int j = 0; j < N; j++ )
{
double[] Ci = C[i];
Ci[j] = ( A[i][k] * B[k][j] ) + Ci[j];
}
}
}
return C;
}
}
}
BenchmarkDotNet=v0.10.11, OS=Windows 10 Redstone 1 [1607, Anniversary Update] (10.0.14393.1944)
Processor=Intel Xeon CPU E3-1270 v5 3.60GHz, ProcessorCount=8
Frequency=3515629 Hz, Resolution=284.4441 ns, Timer=TSC
  [Host]    : .NET Framework 4.7 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.7.2558.0
  LegacyClr : .NET Framework 4.7 (CLR 4.0.30319.42000), 64bit LegacyJIT/clrjit-v4.7.2558.0;compatjit-v4.7.2558.0
  RyuClr    : .NET Framework 4.7 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.7.2558.0

MaxRelativeError=0.01  Platform=X64  Runtime=Clr  
LaunchCount=5  RunStrategy=ColdStart  TargetCount=1  
UnrollFactor=1  WarmupCount=1  
Method Job Jit Mean Error StdDev Scaled ScaledSD
Jagged LegacyClr LegacyJit 270.1 ms 11.969 ms 3.109 ms 1.00 0.00
JaggeFromCpp LegacyClr LegacyJit 331.0 ms 8.628 ms 2.241 ms 1.23 0.01
Single LegacyClr LegacyJit 328.7 ms 9.329 ms 2.423 ms 1.22 0.01
Standard LegacyClr LegacyJit 548.2 ms 22.009 ms 5.717 ms 2.03 0.03
UnsafeSingle LegacyClr LegacyJit 289.9 ms 8.085 ms 2.100 ms 1.07 0.01
Jagged RyuClr RyuJit 500.1 ms 20.247 ms 5.259 ms 1.00 0.00
JaggeFromCpp RyuClr RyuJit 557.2 ms 7.820 ms 2.031 ms 1.11 0.01
Single RyuClr RyuJit 541.9 ms 4.381 ms 1.138 ms 1.08 0.01
Standard RyuClr RyuJit 1,202.8 ms 45.570 ms 11.837 ms 2.41 0.03
UnsafeSingle RyuClr RyuJit 364.8 ms 5.254 ms 1.365 ms 0.73 0.01
BenchmarkDotNet=v0.10.11, OS=Windows 10 Redstone 1 [1607, Anniversary Update] (10.0.14393.1944)
Processor=Intel Xeon CPU E3-1270 v5 3.60GHz, ProcessorCount=8
Frequency=3515629 Hz, Resolution=284.4441 ns, Timer=TSC
.NET Core SDK=2.1.2
  [Host]  : .NET Core 2.0.3 (Framework 4.6.25815.02), 64bit RyuJIT
  RyuCore : .NET Core 2.0.3 (Framework 4.6.25815.02), 64bit RyuJIT

Job=RyuCore  MaxRelativeError=0.01  Jit=RyuJit  
Platform=X64  Runtime=Core  LaunchCount=5  
RunStrategy=ColdStart  TargetCount=1  UnrollFactor=1  
WarmupCount=1  
Method Mean Error StdDev Scaled ScaledSD
Jagged 500.8 ms 13.912 ms 3.6135 ms 1.00 0.00
JaggeFromCpp 561.5 ms 11.437 ms 2.9708 ms 1.12 0.01
Single 602.4 ms 3.150 ms 0.8182 ms 1.20 0.01
Standard 1,205.6 ms 20.636 ms 5.3600 ms 2.41 0.02
UnsafeSingle 368.5 ms 14.193 ms 3.6866 ms 0.74 0.01
namespace MatrixMultiplication
{
public static class UnsafeSingle
{
public static unsafe double[] Multiply( int N )
{
var C = new double[N * N];
var A = new double[N * N];
var B = new double[N * N];
Util.Initialize( N, A, B, C );
fixed ( double* ptrA = A )
{
fixed ( double* ptrB = B )
{
fixed ( double* ptrC = C )
{
for ( int i = 0; i < N; i++ )
{
for ( int k = 0; k < N; k++ )
{
for ( int j = 0; j < N; j++ )
{
ptrC[i * N + j] += ptrA[i * N + k] * ptrB[k * N + j];
}
}
}
}
}
}
return C;
}
}
}
namespace MatrixMultiplication
{
internal static class Util
{
internal static void Initialize( int N, double[][] A, double[][] B, double[][] C )
{
int index = 0;
do
{
C[index] = new double[N];
B[index] = new double[N];
A[index] = new double[N];
int column = 0;
int value = 0;
do
{
C[index][column] = 0;
double num7 = value; // used so we only cast once
B[index][column] = num7;
A[index][column] = num7;
column++;
value = index + value;
} while ( column < N );
index++;
} while ( index < N );
}
internal static void Initialize( int N, double[] A, double[] B, double[] C )
{
for ( int i = 0; i < N; i++ )
{
for ( int j = 0; j < N; j++ )
{
C[i * N + j] = 0;
B[i * N + j] = i * j;
A[i * N + j] = i * j;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment