Significant overhead when working with .NET methods in Matlab.
I did a little test in Matlab (8.0.0.783 (R2012b)):
v = zeros(10000,1); for i=1:3 rnd = System.Random(); tic; for j=1:10000, v(j) = rnd.NextDouble(); end; toc; dt = System.DateTime(2014,1,28,0,0,0); tic; for j=1:10000, dt = dt.AddSeconds(1); end; toc; end
This applies to Matlab in my PC application. 0.5 seconds for the primary circuit and 1 second for the secondary circuit. In pure .NET code, which takes 0.00015 and 0.0002 seconds. Thus, the overhead of calling a .NET object method in Matlab depends on the object and the .NET method at hand.
For more complex .NET object methods, the overhead can be even worse. I am responsible for the .NET API for accessing files in a specialized scientific data format.
This .NET API can be used from Matlab. In the worst case, you read only one double or float value each time you call the .NET reading method (time series files: for each moment there is one value (double or float) for several elements).
A script to read such a file shows that Matlab on my laptop can make slightly less than 1,000 calls in .NET per second.
The funny thing is that if I put the same code in the Matlab function (basically puts the read_file () function as the first line in the script), Matlab makes about 6.500 .NET calls per second. Thus, inside the Matlab function is about 8 times faster than inside the script when it comes to calling these .NET methods. This does not reproduce with the above test case.
The bottom line is that when calling the .NET method from inside Matlab, a significant overhead occurs. It is important to make the .NET api "chunky" instead of "chatty".
We solved our problems by creating a set of "chank helper" methods in the .NET dll utility that does all the reading, collects data in a large matrix and returns the matrix in a single Matlab call, mainly minimizing activity over the Matlab-NET border.
Suitable .NET code seems to be as fast when starting from a clean .NET application, as when starting from Matlab.