MATLAB Performance
Optimize performance of your MATLAB code
Optimize performance of your MATLAB code
The MATLAB® execution engine, introduced in R2015a, uses JIT compilation to accelerate all MATLAB code. JIT compilation generates native machine level code that is optimized for your MATLAB code and for specific hardware. The architecture of the execution engine enables further optimizations with each release, including faster calls to built-in functions and faster indexing operations. In addition, many core MATLAB functions are implicitly multithreaded for better performance.
MATLAB performance is measured using a set of benchmarks that cover both unit operations and complete applications that represent real user workflows. Those benchmarks run multiple times during a MATLAB release cycle on different hardware and operating systems to validate new optimizations, detect and address performance regressions, and identify operating system specific issues.
You can learn about specific performance improvements in the MATLAB release notes. Since MATLAB R2019b, performance release notes include measured improvements in execution time compared to previous MATLAB versions.
The first step to improve the performance of your code is to identify bottlenecks. For instance, you can:
tic
, toc
, and timeit
Use the MATLAB Profiler to see which parts of your program take the most time to runOnce you have identified bottlenecks in your code, known programming practices can often make your code faster. Two of the most commonly used techniques are array preallocation and vectorization. Preallocation can improve performance by avoiding dynamic memory allocation. Vectorization enables you to avoid loops by operating on all the elements of a vector in a single command. Together, these techniques can speed up code by several orders of magnitude.
If necessary, you can increase performance by writing computationally demanding parts of your application in a compiled language. In MATLAB, MEX functions enable you to call high-performance C, C++, or Fortran code just like MATLAB built-in functions. With MATLAB Coder™, you can automatically convert your MATLAB code into MEX files, which may run much faster.
You can solve computationally- or data-intensive problems by using parallel computing to explicitly access all of your hardware resources. You can leverage functionality to scale to multiple processes, multiple threads, and GPUs all with the familiarity and ease of use of MATLAB. You can develop and run on a single machine, and you can scale your execution to a compute cluster or cloud without needing to recode.