Note that you're benchmarking a routine that takes roughly 100ns (1s = 1'000'000'000ns). For such short operations at some point the p/invoke and marshaling overhead to call from managed code into native code will dominate the timing entirely. If you only need short operations like this (as opposed to, say, large matrix multiplications or decompositions) then you may indeed end up with better performance with the managed provider.
Also, your code still includes the first call (which includes JIT, MKL init etc) which will dominate the average for both providers. A small modification testing the same thing but measuring the first call separately (as Init) typically shows results like this on my machine for MKL:
Thanks,
Christoph
Also, your code still includes the first call (which includes JIT, MKL init etc) which will dominate the average for both providers. A small modification testing the same thing but measuring the first call separately (as Init) typically shows results like this on my machine for MKL:
Intel MKL (x64; revision 4)
Create Data: 00:00:00.0127491s
Init: 2363400.000ns
A: 101.825ns
A: 102.201ns
A: 102.026ns
A: 101.821ns
A: 102.509ns
And for managed:Managed
Create Data: 00:00:00.0128457s
Init: 1948500.000ns
A: 168.284ns
A: 167.912ns
A: 168.293ns
A: 168.173ns
A: 168.143ns
Code:const int N = 100000;
Control.UseManaged();
//Control.UseNativeMKL();
Console.WriteLine(Control.LinearAlgebraProvider.ToString());
var w = Stopwatch.StartNew();
var uniform = new ContinuousUniform(-0.5, 0.5)
var a = Vector<double>.Build.Random(100, uniform);
var b = Vector<double>.Build.Random(100, uniform);
Console.WriteLine("Create Data: {0}s", w.Elapsed);
// we accumulate the results to make sure the compiler does not optimize it away.
w.Restart();
double x = a*b;
Console.WriteLine("Init: {0:0.000}ns", (w.Elapsed.TotalMilliseconds*1000*1000));
for (int k = 0; k < 5; k++)
{
w.Restart();
for (int i = 0; i < N; i++)
{
x += a*b;
}
Console.WriteLine("A: {0:0.000}ns", (w.Elapsed.TotalMilliseconds*1000*1000)/N);
}
Console.WriteLine(x);
Does this clarify things? Or do you see very different numbers in your setup?Thanks,
Christoph