MATLAB Programming
MATLAB Programming MATLAB Programming
11 Improving Performance and Memory Usage Techniques for Improving Performance This section covers the following suggestions on how you can improve the performance of your MATLAB programs: • “Multithreaded Computation in MATLAB” on page 11-4 • “Vectorizing Loops” on page 11-4 • “Preallocating Arrays” on page 11-7 • “Coding Loops in a MEX-File” on page 11-9 • “Assigning to Variables” on page 11-9 • “Operating on Real Data” on page 11-10 • “Using Appropriate Logical Operators” on page 11-10 • “Overloading Built-In Functions” on page 11-11 • “Functions Are Generally Faster Than Scripts” on page 11-11 • “Load and Save Are Faster Than File I/O Functions” on page 11-11 • “Avoid Large Background Processes” on page 11-11 Multithreaded Computation in MATLAB One way of achieving improved performance when running MATLAB on multicoreormultiprocessorsystemsisto take advantage of its multithreaded computational capabilities. To enable multithreaded computation in MATLAB, see “Multithreading Preferences” in the “Desktop Tools and Development Environment” documentation. Vectorizing Loops MATLAB is a matrix language, which means it is 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. Simple Example of Vectorizing Here is one way to compute the sine of 1001 values ranging from 0 to 10: 11-4
Techniques for Improving Performance i = 0; for t = 0:.01:10 i = i + 1; y(i) = sin(t); end A vectorized version of the same code is t = 0:.01:10; y = sin(t); The second example executes much faster than the first and is the way MATLAB is meant to be used. Test this on your system by creating M-file scripts that contain the code shown, and then using the tic and toc functions to time the M-files. Advanced Example of Vectorizing repmat is an example of a function that takes advantage of vectorization. It accepts three input arguments: an array A, a row dimension M, andacolumn dimension N. repmat creates an output array that contains the elements of array A, replicated and “tiled” in an M-by-N arrangement: A = [1 2 3; 4 5 6]; B = repmat(A,2,3); B = 1 2 3 1 2 3 1 2 3 4 5 6 4 5 6 4 5 6 1 2 3 1 2 3 1 2 3 4 5 6 4 5 6 4 5 6 repmat uses vectorization to create the indices that place elements in the output array: function B = repmat(A, M, N) % Step 1 Get row and column sizes [m,n] = size(A); 11-5
- Page 714 and 715: 9 Classes and Objects The Stock sub
- Page 716 and 717: 9 Classes and Objects case '.' swit
- Page 718 and 719: 9 Classes and Objects Example — T
- Page 720 and 721: 9 Classes and Objects if isa(name,'
- Page 722 and 723: 9 Classes and Objects end if saving
- Page 724 and 725: 9 Classes and Objects Saving and Lo
- Page 726 and 727: 9 Classes and Objects • subsref
- Page 728 and 729: 9 Classes and Objects function n =
- Page 730 and 731: 9 Classes and Objects Object Preced
- Page 732 and 733: 9 Classes and Objects How MATLAB De
- Page 734 and 735: 9 Classes and Objects Private funct
- Page 736 and 737: 9 Classes and Objects 9-76
- Page 738 and 739: 10 Scheduling Program Execution wit
- Page 740 and 741: 10 Scheduling Program Execution wit
- Page 742 and 743: 10 Scheduling Program Execution wit
- Page 744 and 745: 10 Scheduling Program Execution wit
- Page 746 and 747: 10 Scheduling Program Execution wit
- Page 748 and 749: 10 Scheduling Program Execution wit
- Page 750 and 751: 10 Scheduling Program Execution wit
- Page 752 and 753: 10 Scheduling Program Execution wit
- Page 754 and 755: 10 Scheduling Program Execution wit
- Page 756 and 757: 10 Scheduling Program Execution wit
- Page 758 and 759: 10 Scheduling Program Execution wit
- Page 760 and 761: 10 Scheduling Program Execution wit
- Page 762 and 763: 11 Improving Performance and Memory
- Page 766 and 767: 11 Improving Performance and Memory
- Page 768 and 769: 11 Improving Performance and Memory
- Page 770 and 771: 11 Improving Performance and Memory
- Page 772 and 773: 11 Improving Performance and Memory
- Page 774 and 775: 11 Improving Performance and Memory
- Page 776 and 777: 11 Improving Performance and Memory
- Page 778 and 779: 11 Improving Performance and Memory
- Page 780 and 781: 11 Improving Performance and Memory
- Page 782 and 783: 11 Improving Performance and Memory
- Page 784 and 785: 11 Improving Performance and Memory
- Page 786 and 787: 12 Programming Tips MATLAB Path (p.
- Page 788 and 789: 12 Programming Tips numberOfLines)
- Page 790 and 791: 12 Programming Tips Help This secti
- Page 792 and 793: 12 Programming Tips Topic Name arit
- Page 794 and 795: 12 Programming Tips Youcangethelpon
- Page 796 and 797: 12 Programming Tips Commenting Out
- Page 798 and 799: 12 Programming Tips For M-file func
- Page 800 and 801: 12 Programming Tips Function Argume
- Page 802 and 803: 12 Programming Tips Passing Argumen
- Page 804 and 805: 12 Programming Tips For more inform
- Page 806 and 807: 12 Programming Tips Debugging This
- Page 808 and 809: 12 Programming Tips Setting Breakpo
- Page 810 and 811: 12 Programming Tips Variables This
- Page 812 and 813: 12 Programming Tips AvoidUsingiandj
Techniques for Improving Performance<br />
i = 0;<br />
for t = 0:.01:10<br />
i = i + 1;<br />
y(i) = sin(t);<br />
end<br />
A vectorized version of the same code is<br />
t = 0:.01:10;<br />
y = sin(t);<br />
The second example executes much faster than the first and is the way<br />
<strong>MATLAB</strong> is meant to be used. Test this on your system by creating M-file<br />
scripts that contain the code shown, and then using the tic and toc functions<br />
to time the M-files.<br />
Advanced Example of Vectorizing<br />
repmat is an example of a function that takes advantage of vectorization. It<br />
accepts three input arguments: an array A, a row dimension M, andacolumn<br />
dimension N.<br />
repmat creates an output array that contains the elements of array A,<br />
replicated and “tiled” in an M-by-N arrangement:<br />
A = [1 2 3; 4 5 6];<br />
B = repmat(A,2,3);<br />
B =<br />
1 2 3 1 2 3 1 2 3<br />
4 5 6 4 5 6 4 5 6<br />
1 2 3 1 2 3 1 2 3<br />
4 5 6 4 5 6 4 5 6<br />
repmat uses vectorization to create the indices that place elements in the<br />
output array:<br />
function B = repmat(A, M, N)<br />
% Step 1 Get row and column sizes<br />
[m,n] = size(A);<br />
11-5