8

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'
Community
  • 1
  • 1
Nathan Donnellan
  • 573
  • 3
  • 11
  • 3
    At the end of all the answers you get to this you'll be left thinking 'hmm, there's no way of deciding, by argument alone, how much overhead is associated with calling .NET objects from within Matlab, I'll have to measure it for my application on my systems'. You might as well start measuring the overhead now. – High Performance Mark Mar 28 '12 at 16:56
  • I actually did do a brief study I can share, but I have less than 100 rep, so I can't answer my own question yet. xP – Nathan Donnellan Mar 28 '12 at 16:58
  • I look forward to seeing your data, can't you edit your question to include it ? – High Performance Mark Mar 28 '12 at 17:01
  • +1 for injecting some data into the discussion. Pity no one else is joining in ! – High Performance Mark Mar 28 '12 at 22:33
  • In terms of performance, have you profiled the code? I've found very few cases where the cost of dispatching is the real bottleneck. There's usually some other, higher-level optimization that makes the whole algorithm run faster. There are also a number of easy, low-level optimizations that the Matlab JIT compiler cannot always do. For example, it can be quite expensive to access an object property in a loop. I've accelerated loops by a factor of 10x or more just by assigning object properties to local variables. I only knew to do that after I profiled the code. – sfstewman Jun 29 '12 at 02:08
  • Note that it is up to the .NET garbage collector to remove .NET objects: http://www.mathworks.se/help/techdoc/matlab_external/brpb58s-1.html#bshoa6b-1 So, but in my experience all .NET objects I have created stayed for as long as the MATLAB environment was open. This might be due to the .NET code, as I haven't completely checked that out, but you might want to keep this in mind anyways. – Juhl Jul 03 '12 at 14:53
  • Interesting, I might have to see how long my objects hang around. – Nathan Donnellan Jul 18 '12 at 13:47

2 Answers2

4

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.

0

A running application consumes resources to recalculate and repaint data using GD I/GD I+ tools. Both operations are not linked with each other. Recalculation means sorting, filtering, grouping and all other operations except repainting.

JSK NS
  • 3,346
  • 2
  • 25
  • 42