0

my C S-function is faster than my embedded Matlab function block in Matlab environment but when I use it in RT-Lab, the embedded is faster!

does any one know why? I just used necessary routines not the optional ones.

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
ilegna
  • 43
  • 6
  • How are you measuring execution time (particularly in simulation)? Are you sure that you're not including compilation time (particularly for the MATLAB Function block)? Depending on various things that can be done to customize the build process, in real-time I would expect the Embedded MATLAB to possibly be faster as it involves one function call where as the S-Function may involve several. However, I wouldn't expect that slight difference to be particularly noticeable. – Phil Goddard Dec 30 '13 at 18:44
  • hello Dear @Phil and thanks for your response. as you know, C codes are so faster than Mfiles, even if it's a simple command. so C S-Function is faster than an Embedded Matlab function, as it is for me in Matlab. the difference is as much as you can feel it with out computing. actually to get the ex. time, I used two models named, Embedded & Sfun, then for Embedded: clc tic sim Embedded tE=toc; & I gave the same path to Sfun and: clc tic sim Sfun tS=toc; but when I use them in a RT-Lab model (in parallel), their execution time is near together, and even the EMF is faster than C S-Function! – ilegna Jan 02 '14 at 18:28
  • I use the monitoring block in RT-Lab, and as ex.time, I use the port named "event execution time". – ilegna Jan 02 '14 at 18:30
  • 1
    You seem to be under a misapprehension of how the MATLAB Function block works. The m-code is converted to C then compiled and executed in simulation. Pointing out that C runs faster than m-code is irrelevant, as both the S-Fcn approach and Embedded MATLAB approach are running compiled C-code. In my previous comment I asked how you were timing the result as there's a good chance that you are including the time to convert the m-code to c-code in your timing, and hence the MATLAB Fcn block looks slower. Since you are using tic and toc to do the timing I suspect that is indeed the case. – Phil Goddard Jan 02 '14 at 20:47
  • seems you're right about time in matlab environment, I didn't pay attention to that.as RT-Lab generates the C code of the model during its compilation step, at 1st I though that there should be no difference between an EMF and Sfun in execution time, but seeing "http://www.opal-rt.com/kb-article/using-embedded-matlab-function-vs-s-function" made me think that a C S-Function block must be faster than the embedded block.am I right? if not, could you plz explain more? (of course there is an explanation about it in my mind but it may be wrong, so I don't want to orient your mind :) ) thanks @phil – ilegna Jan 03 '14 at 00:39
  • In most instances you will not be able to detect any real speed difference. Nominally the MATLAB Fcn block contains a single function call, while an S-Function contains multiple function calls. If function calls are expensive (in terms of time) on your RTOS then S-Functions may be slower, but this will/should be so small compared to the actual functionality implemented in the code that I wouldn't base my design decision on it. – Phil Goddard Jan 03 '14 at 06:44
  • so @Phil, what's the point to use s-functions while writing C codes is more complex than m-codes? what's the gain for this pain? PS. did you visit the link I introduced you in my pre-comment? what was your opinion? – ilegna Jan 03 '14 at 16:52
  • 1
    The link in your previous comment doesn't work. You can do much more with an S-Function than with a MATLAB Function block. For instance you can have continuous states and you can interface with other C functionality easier. Typically you'll use a MATLAB Fcn block if you can. If you can't you'll use an S-Function. – Phil Goddard Jan 04 '14 at 00:20
  • so in EMF we can't have continuous states, (honestly I don't get this completely, could you plz give me an example?). I see :). it didn't work cuz I put it in quotation marks and the site made the link with them. this link works, so plz send me your feedback. http://www.opal-rt.com/kb-article/using-embedded-matlab-function-vs-s-function thanks @Phil . – ilegna Jan 04 '14 at 10:05
  • 1 more question too, is there any way to calculate the exact time of just execution of a block? – ilegna Jan 05 '14 at 00:48
  • 1
    See the Simulink Profiler: http://www.mathworks.com/help/simulink/ug/capturing-performance-data.html – Phil Goddard Jan 05 '14 at 15:57

0 Answers0