A few days ago I happened to speak with a colleague about Simulink run-time performance, and we discussed various ideas for simulation speed-up. This topic is very important to MathWorks, as evidenced by a dedicated documentation section, newsletter articles (1, 2), webinars (1, 2, 3, 4) and multiple blog posts on improving simulation performance using the Simulink product.

This blog covers mainly the Matlab core product and closely-related toolboxes. However, since the various suggestions for improving performance are spread across so many resources, I thought that it would be worthwhile to create a post listing all the suggestions in a single place. When faced with a slow simulation, it’s nice to know that there are so many available speed-up options, so I hope readers will find it useful. Note that these suggestions are in the large part fully documented and supported. The ideas are listed based on semantic relationship, and not by order of importance:

- Select Accelerator or Rapid Accelerator simulation mode (also see here)
- Enable the options for Compiler optimization, automatic Block Reduction and others in the Optimization pane of the Configuration Parameters window.
- Switch the Compiler optimization from
*faster build*to*faster run*. - Disable the options for debugging/animation support, overflow detection and echoing expressions without semicolons in the Simulation Target pane of the Configuration Parameters window. Note that overflow detection is important for fixed-point data so if your model includes FP data, keep this option enabled.
- Disable similar configuration options in Stateflow’s Configuration Parameters window (which is similar but separate from the Simulink window).
- Keep Stateflow charts and Simulink blocks/models closed during simulation to prevent run-time updates (this relates to the animation feature, if enabled)
- Keep output scopes closed during simulation run-time; open the scopes only after the simulation ends. If a scope must remain open in run-time, reduce the number of data points (via decimation, reduced time range and reduced number of plotted signals), increase the plotting refresh period, disable scope scrolling, remove data markers and legends, limit the history size, and use reduced fidelity in the viewer parameters. A similar recommendation applies to regular Matlab plots, by the way.
- Vectorize processing by combining multiple signals into a vector and applying the processing on the vector rather than on the separate signals.
- Sample data in batches (rather than one-by-one) using frame-based processing (R2011b and newer). This can speed up simulations by 10x or more, at the expense of just a little extra memory.
- Aggregate small blocks into larger ones.
- Load the model in memory using
instead of**load_system**and simulate it using the**open_system**command, then post process/display the outputs.**sim** - Avoid blocks that do not support code generation, since they would have to be run in slower interpreted mode.
- If your simulation does not use linear algebra (matrix arithmetic), disable BLAS library support in the Simulation Target pane. But if it does use it, then ensure that BLAS is enabled for optimal performance.
- Avoid algebraic loops where possible.
- Avoid
`Matlab S-Function`

and`Interpreted Matlab Function`

blocks. Instead, use`Matlab Function`

,`System`

and`C-MEX`

blocks.

Note: IMHO, the naming of these blocks is quite confusing:

`Matlab Function`

refers to a subset of the Matlab language previously called`Embedded Matlab`

that can be directly converted into C-code without requiring the Matlab run-time (MCR); this is basically the language subset used by the Matlab Coder Toolbox.`Interpreted Matlab Function`

refers to the full Matlab functionality and requires the Matlab run-time (MCR) – it was previously called`Matlab Function`

. - Reduce, simplify or eliminate initialization and termination phases.
- Use the Mask Editor to reduce block images resolution and size.
- Store configurations in loadable MAT files rather than programmatically.
- Consolidate multiple
calls into a single call having several name / value pairs.**set_param** - Use a stronger platform (esp. increase the amount of memory).
- Limit or disable log output, and disk I/O in general.
- Use the
and**sldiagnostics**functions and the Simulink Profiler, to identify and eliminate simulation bottlenecks.**slprofreport** - Use the
function and the Simulink Model Performance Advisor to automatically adjust model configurations for best performance (R2012b and newer; also see this webinar).**performanceadvisor** - Manually adjust model solver parameters for optimal performance, based on a-priori knowledge about the model behavior.
- Use a faster solver that can still process the model. Different models may require different solvers to run optimally (also see here).
- Tune solver parameters (decrease solver order, increase step size and error tolerance) to improve solution convergence speed at the expense of some accuracy. One user reported a 20x speedup by simply increasing the step-size.
- Prevent excessive zero-crossing or disable zero-crossing detection (also see here and here).
- Use simpler models or models with reduced fidelity.
- Store the simulation state at specific points, then load these states for repeated later simulations, thus saving the simulation time of the loaded portion.
- Run simulations in parallel using Parallel Computing Toolbox (also see here, here and here). An easy way to do this is to run the
command within a**sim**loop.**parfor** - Move some calculations onto FPGA or GPU.
- Use a real-time system and set simulation speed to real-time (high priority).

Some additional ideas can be found in the blog posts and webinars mentioned in the hyperlinks at the top of this post. One of the newsletter articles concludes with an example of how applying some of these techniques to a specific model resulted in reduced simulation time, from 453 down to 5 seconds.

I would like to take this opportunity to congratulate MathWorks on a job well done with Simulink’s Performance Advisor. I expect a variant of this tool to be available for regular Matlab m-code in some future Matlab release; it would blend in nicely with the existing mlint (Code Analyzer) tool.

### Advanced Matlab Programming course – London 10-11 March, 2014

If this topic has piqued your interest, consider joining my Advanced Matlab Programming course in London on 10-11 March, 2014. In this course/seminar I will explore numerous other ways by which we can improve Matlab’s performance and create professional code. This is a unique opportunity to take your Matlab skills to a higher level within a couple of days.

I would appreciate any advice on speeding up

mexCallMatlabfunctionality. At the moment I have a fortran mex function that calls a Matlab function viamexCallMatlab, that then calls the derivative values of a Simulink model. This is painfully slow and I am trying to figure out how to write a TLC script that can generate C-code from the Simulink model in the correct format, so that I can avoidmexCallMatlaball together, but the TLC functionality is not very well documented as far as I can tell.@Etienne – perhaps you haven’t looked close enough? I think that TLC is actually extensively documented here. If this is not enough, then there is also a quick reference and even a 340-page PDF document (which is admittedly quite old). I can’t vouch for the

qualityof the documentation as I’ve never used TLC, but at least judging by thequantity, reading all of it should keep you busy for quite some time…Thanks Yair.