I'm interfacing MATLAB with C/C++ using the MATLAB Engine API.
In my particular case MATLAB is used to calculate something and result is printed in C. However, throughout various test on both sides I noticed significant performance losses in C.
Here is an example of the MATLAB function calls:
tic;
data = predictIM(data);
toc;
On the C side I call similar functions as follows:
iMod::Timer_T<high_resolution_clock> t;
engPutVariable(ep, "data", dataContent);
engEvalString(ep, "[posture] = predictIM(data);");
UT_NOTIFY(LV_DEBUG,"The execution took "<<t.seconds());
My timer implementation in C++ looks as follows:
template< class Clock >
class Timer_T
{
typename Clock::time_point start;
public:
Timer_T() : start( Clock::now() ) {}
typename Clock::duration elapsed() const {
return Clock::now() - start;
}
double seconds() const {
return elapsed().count() *
((double)Clock::period::num/Clock::period::den);
}
};
The above MATLAB code runs at approximately 180 frames per second including setting the matrix (data
), whereas the C code only at 24 FPS. I used tic
/toc
to measure the execution time in MATLAB whereas my own timer implementation is used on the C/C++ side.
While profiling the application I noticed that the MATLAB Engine calls are the bottleneck. I know that the Linux MATLAB Engine implementation is using named pipes for interfacing with MATLAB and I was wondering if there is a way to speed up the communication of MATLAB with its Engine?