S-function performance in SLDRT external mode

2 views (last 30 days)
Why does my S-function run faster in SLDRT normal mode than external mode?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 21 Jun 2023
Edited: MathWorks Support Team on 21 Jun 2023
S-function has very limited support in external mode. Try inline S-functions, to see if it boosts the performance. With that said, see the following points:
  • Code in External mode is compiled using a different compiler with different optimization capabilities and running in entirely different environment. Strictly speaking, there is no reason for the performance of the code to be identical or even similar, except for the fact that different compilers use similar optimizations after all, and that the environment (runtime library, OS support) usually has similar performance when doing similar tasks. Apparently, your S-function might be using something that is much slower in External mode compiled code and/or libraries than it is in Normal mode code. This will be difficult to locate though. Potential candidates are math functions (sin, cos, exp, ...), string manipulation functions, or any library function in general, provided that it is called many times during the mdlOutputs or mdlUpdate method (or model_step if they are using this code format). Most likely, this will happen in the innermost loop if there are nested loops in the code.
  • The only way to diagnose this would be disable pieces of code in the S-function (replacing the code by some dummy operation like returning a constant) until the piece of code responsible for the slow performance is found - starting with bigger pieces, then refining to a group of functions or even to a single function. I understand that this is a time-consuming iterative process, but there are not many alternatives.
  • External mode has been never designed for long-running tasks. Let's say a task taking 10 seconds is already too much for a single task to execute. Often times, a long running task is aborted by Windows on the assumption that the CPU has stopped responding. The maximum execution time of a single External mode task is about 1 second; for longer tasks, Normal mode is recommended otherwise.
You can read about inline S-functions and their performance here:

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!