Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET performance from Matlab

I have a large simulation suite written in Matlab, but due to concerns about better interfacing with other internal projects (as well execution speed) I'm thinking about moving some functionality to .NET and calling such objects from within Matlab. What is the overhead associated with calling .NET objects from within Matlab?

Here's a good discussion on Matlab OO that doesn't talk about .NET

Edit: Brief study

I ran a quick test on my own from within Matlab of simple access and assignment operations within different objects including formal Matlab objects (R2011b), Java and .NET calling each 1,000,000 times. The method calls refer to internal looping, the property/field calls refer to accessing the public field from Matlab and looping in Matlab. The last results puzzle me as the overhead for .NET is much higher than Java but the actual run-time is about half. What is going on?

    Access(s)  Assign(s)  Type of object/call
    --- MATLAB ---
    0.003361   0.004268   'myObj.field'
    0.003403   0.004263   'myStruct.field'
    0.003376   0.003392   'myVar'   
    0.152629   0.303579   'myHandleObj.field'
    25.79159   -          'TestConstant.const'
    0.003384   -          'myTestConstant.const' (instance)
    0.006794   0.008689   'TestObj.methods'
    0.157509   0.303357   'TestHandleObj.methods'

    --- NON-MATLAB ---
    10.70006   16.42527   'JavaObj fields'
    0.005063   0.005441   'JavaObj methods'
    43.49988   43.96159   'NetObj fields'
    0.002194   0.002306   'NetObj methods'
like image 298
Nathan Donnellan Avatar asked Mar 28 '12 16:03

Nathan Donnellan


1 Answers

There is a significant overhead when working with .NET methods in Matlab.

I did a small 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

That takes in Matlab on my PC app. 0.5 seconds for the first loop and 1 second for the second loop. In pure .NET code that takes 0.00015 and 0.0002 seconds. So the overhead when calling a .NET object method in Matlab depends on the .NET object and method at hand.

For more complex .NET object methods, the overhead can be even worse. I am responsible for a .NET API for accessing files in a specialized scientific data format.

This .NET API can be used from within Matlab. In worse case you only read one double or float value every time calling a .NET read method (time-series files: For every time there is one value (double or float) for a number of items).

A script for reading such a file shows that Matlab on my laptop can do somewhat less than 1.000 calls to .NET per second.

The funny thing is that if I put the same code into a Matlab function (basically puts function read_file() as the first line in the script), Matlab does about 6.500 .NET calls per second. So inside a function, Matlab is about 8 times faster than inside a script, when it comes to calling those .NET methods. That is not reproducible with the test example above.

Bottom line is that there is a significant overhead in calling a .NET method from within Matlab. It is important to make the .NET api "chunky" instead of "chatty".

We have solved our issues by creating a set of "chunky helper" method in a utility .NET dll, that does all the reading, collects data in a big matrix, and returns the matrix in one call to Matlab, basically minimizing the activity over the Matlab-.NET boundary.

The underlying .NET code seems to be as fast when run from within a pure .NET application as when run from within Matlab.

like image 102
Jesper Grooss Avatar answered Sep 23 '22 14:09

Jesper Grooss