High calling overhead when calling compiled functions from C++ application (V4.0 Compiler)


 I have just started using V4.0 of the compiler and am doing some
performance testing.

 I am interested particularly in calling Matlab functions from a C++
program. From my initial tests, I am concerned about the cost of
invoking a function.

 Calling a compiled version of the the following minimal function:

function [ out ] = return_input( in )
out = in;

....from a C++ application takes an average of 15 ms per call
(averaged over 1000 calls).

As a comparison from Matlab console, it takes 6 microseconds (1/2500
of the time).

for ( i=1:1000000 )
return_input( 99.9 );

Elapsed time is 6.610000 seconds.

An observation of what happens when calling from C++ is that it does
not seem to be CPU bound (CPU usage is down at 1-2% during the test).

Has anyone else noticed this? I am confused as to what is causing
this overhead - I see minimal CPU usage, no page-faulting, no I/O
reads or writes.

As a more minor point, the 1.5 - 2 seconds it takes to initialise the
runtime seems a bit excessive (but maybe this is because I am used to
previous versions of the Matlab compiler, where this wasn't an issue
at all ;)

   Thanks in advance,

12/2/2004 10:50:25 AM
comp.soft-sys.matlab 211266 articles. 22 followers. lunamoonmoon (257) is leader. Post Follow

3 Replies

Similar Articles

[PageSpeed] 58

Graham wrote:
> Hi,
> I have just started using V4.0 of the compiler and am doing some
> performance testing.

.... Update - I took the latest download of the compiler (4.1.1) and
it made no difference.
12/2/2004 12:30:57 PM
Has anyone noticed this? Has anyone from Mathworks got any insights,
because it seems like a serious issue to me?
12/3/2004 2:25:03 PM
Further observations...

When you initialize the Matlab Runtime, it takes 1.5-2 seconds and
creates ***12 new threads***.

When you call a "compiled" function from C++ it either switches
thread to execute it or at least sychronises with another thread
before executing it (its hard to see from the debugger). This would
explain the overhead and the low CPU utilisation would be due to the
fact that the thread goes into a wait state at some point during

Can anyone confirm/refute this? ... and the implcation that the new
compiler is just not suitable for functions that are quick to

12/6/2004 12:50:43 PM