Significant Increase in Computation Time After Updating S-Function in Simulink

3 views (last 30 days)
I have a multibody simulation model in Simulink, constructed using Simscape Multibody. Within this model, I've integrated custom C++ code using an S-Function block. Initially, my S-Function had 11 inputs and 6 outputs, as depicted in Figure 1.
Recently, I expanded the C++ code within this S-Function, adding a loop that runs 10,000 times. Consequently, the number of inputs and outputs for the S-Function increased to 25 and 22, respectively. The updated S-Function is illustrated in Figure 2. Additionally, I employed the Memory block in Simulink to use some of the outputs of S-Function as inputs for the subsequent time step itself.
Before introducing the new loop and its associated code, the computation time was 100 seconds for a 1-second simulation duration when run on an AMD 7950X CPU. However, after the update, the computation time surged to 1000 seconds for the same 1-second simulation, all on the identical hardware. This perplexes me, as executing the added code separately in another C++ IDE took only 2 seconds.
For additional context, both the S-Function and my standalone C++ IDE utilize the MSVC compiler with the 'O2' optimization flag.
Figure 1 Figure 2

Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!