I'm working on a little side project for my big project (an RTS in managed code, just for fun, nothing serious) and have been benchmarking several early approaches to the math problem.
Managed DirectX contains classes for matrix, quaternions, vectors, etc and uses d3dx internally to get things done. Since I want my project to run on several target platforms, but still maintain the best possible performance, I've been benchmarking both .NET math code and managed directx' own implementations. Here I get some odd benchmark results...
The easiest way, using methods on a MDX matrix object:
public bool Execute()
{
o = Matrix.Multiply(m, n);
return true;
}
................!
Took: 0h, 0m, 1.656s. for 8000000 iterations.
Iters per ms: 4830,18867924528 - Ms per iter: 0,00020703125
The slightly less easy way, using MDX unsafe native methods:
public bool Execute()
{
unsafe
{
fixed (Matrix* pm = &m) fixed (Matrix* pn = &n) fixed (Matrix* po = &o)
{
UnsafeNativeMethods.Matrix.Multiply(po, pm, pn);
}
}
return true;
}
................!
Took: 0h, 0m, 0.750s. for 8000000 iterations.
Iters per ms: 10666,6666666667 - Ms per iter: 9,375E-05
And the final way, using some very simple .NET matrix code:
public bool Execute()
{
Multiply(ref o, ref m, ref n);
return true;
}
private void Multiply(ref DNMatrix o, ref DNMatrix m, ref DNMatrix n)
{
o.m11 = m.m11 * n.m11 + m.m12 * n.m21 + m.m13 * n.m31 + m.m14 * n.m41;
o.m12 = m.m11 * n.m12 + m.m12 * n.m22 + m.m13 * n.m32 + m.m14 * n.m42;
o.m13 = m.m11 * n.m13 + m.m12 * n.m23 + m.m13 * n.m33 + m.m14 * n.m43;
o.m14 = m.m11 * n.m14 + m.m12 * n.m24 + m.m13 * n.m34 + m.m14 * n.m44;
o.m21 = m.m21 * n.m11 + m.m22 * n.m21 + m.m23 * n.m31 + m.m24 * n.m41;
o.m22 = m.m21 * n.m12 + m.m22 * n.m22 + m.m23 * n.m32 + m.m24 * n.m42;
o.m23 = m.m21 * n.m13 + m.m22 * n.m23 + m.m23 * n.m33 + m.m24 * n.m43;
o.m24 = m.m21 * n.m14 + m.m22 * n.m24 + m.m23 * n.m34 + m.m24 * n.m44;
o.m31 = m.m31 * n.m11 + m.m32 * n.m21 + m.m33 * n.m31 + m.m34 * n.m41;
o.m32 = m.m31 * n.m12 + m.m32 * n.m22 + m.m33 * n.m32 + m.m34 * n.m42;
o.m33 = m.m31 * n.m13 + m.m32 * n.m23 + m.m33 * n.m33 + m.m34 * n.m43;
o.m34 = m.m31 * n.m14 + m.m32 * n.m24 + m.m33 * n.m34 + m.m34 * n.m44;
o.m41 = m.m41 * n.m11 + m.m42 * n.m21 + m.m43 * n.m31 + m.m44 * n.m41;
o.m42 = m.m41 * n.m12 + m.m42 * n.m22 + m.m43 * n.m32 + m.m44 * n.m42;
o.m43 = m.m41 * n.m13 + m.m42 * n.m23 + m.m43 * n.m33 + m.m44 * n.m43;
o.m44 = m.m41 * n.m14 + m.m42 * n.m24 + m.m43 * n.m34 + m.m44 * n.m44;
}
................!
Took: 0h, 0m, 0.750s. for 8000000 iterations.
Iters per ms: 10666,6666666667 - Ms per iter: 9,375E-05
(no, this is not a copy/paste error)
My question, seeing these results is the following: Am I doing something wrong with the MDX calls, since a simple implementation in .net code seems to be 'just as fast' as the MDX implementation... I expected the .net code to be quite a bit slower than the native implamantations...











