Matlab Speed-up skills (from Matlab Help file)

Source: Internet
Author: User

Matlab Speed-up skills (from Matlab Help file)

1. Learn to use Profiler first.
1.1. Open the profiler.
To open the profiler, select View--, profiler from the MATLAB desktop, or type Profiles viewer in the Command Window. The MATLAB Profiler opens.
On my machine is: under Matlab Desktop, Desktop->profiler.
Under the M file editor, Tools->open Profiler.
1.2. Running the profiler
You can copy the code you want to run into the input box after run this code.
Can run this example
[T,y] = Ode23 (' Lotka ', [0 2],[20;20])
You can also enter the M file name to run.
1.3.Click Start Profiling (or press Enter after entering the statement).
1.4. View Profile Detail Report
will tell you which code consumes the time, what functions you can find or which lines of code consume the main time, or are often called.

You can also use the Stopwatch timer function to calculate how long the program consumes
Use Tic and TOC as shown here.
Tic
-Run the program sections to be timed-
Toc


2. Acceleration 1: vectorization
MATLAB is a matrix language, which means it's designed for vector and matrix operations. You can often speed up your m-file code by using vectorizing algorithms that take advantage of this design. Vectorization means converting for and while loops to equivalent vector or matrix operations.

i = 0;
For t = 0:.01:1000
i = i+1;
Y (i) = sin (t);
End

The run time is 30.776 seconds.
Instead, quantify the code:
t = 0:.01:1000;
y = sin (t);
The run time is 0 seconds.

Functions used in vectorizing
Some of the most commonly used functions for vectorizing is:
All
Diff
Ipermute
Permute
Reshape
Squeeze
Any
Find
Logical
Prod
Shiftdim
Sub2ind
Cumsum
Ind2sub
Ndgrid
Repmat
Sort
Sum

3. Accelerating 2:preallocating Arrays (pre-allocated space)
You can often improve code execution time by preallocating the arrays that store output results. Preallocation makes it unnecessary for MATLAB to resize a array each time you enlarge it. Use the appropriate preallocation function for the kind of the array is working with.
Preallocation also helps reduce memory fragmentation if you work with large matrices.

4. Accelerate Other methods:
Coding Loops in a mex-file for speed

If There is instances where you must use a for loop, consider coding the loop in a mex-file. The This is the loop executes much more quickly since the instructions in the loop does not has the to is interpreted each Tim E they execute.

Functions is Faster Than Scripts

Your code would execute more quickly if it was implemented in a function rather than a script. Every time a script is used in MATLAB, it's loaded into memory and uated one line at a time. Functions, on the other hand, is compiled into pseudo-code and loaded into memory once. Therefore, additional calls to the function is faster.

Load and Save is Faster Than File I/O Functions

If you had a choice of whether to use load and save instead of the MATLAB file I/O routines, choose the former. The load and save functions is optimized to run faster and reduce memory fragmentation.

Avoid Large Background Processes

Avoid running large processes in the background at the same time is executing your program in MATLAB. This frees the CPU time for your MATLAB session.

5. Multithreading
In MATLAB desktop, file->preferences->general->multithreading, see if you choose the Enable multithreaded computation.

If not, check it to see if there is speed.

Matlab Speed-up skills (from Matlab Help file)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.