MATLAB Programming
MATLAB Programming MATLAB Programming
11 Improving Performance and Memory Usage As long as the contents of the array are not modified, there is no need to store two copies of it. If you modify the array, then MATLAB does create a separate array to hold the new values. If you modify the array shown above by referencing it with variable A (e.g., A(400,:) = 0), then MATLAB creates a copy of the array, modifies it accordingly, and stores a pointer to the new array in A. Variable B continues to point to the original array. If you modify the array by referencing it with variable B (e.g., B(400,:) = 0), the same thing happens except that it is B that points to the new array. Array Headers When you assign an array to a variable, MATLAB also stores information about the array (such as data type and dimensions) in a separate piece of memory called a header. For most arrays, the memory required to store the header is insignificant. There is a small advantage though to storing large data sets in a small number of large arrays as opposed to a large number of small arrays, as the former configuration requires fewer array headers. Structure and Cell Arrays. For structures and cell arrays, MATLAB creates a header not only for each array, but also for each field of the structure and for each cell of a cell array. Because of this, the amount of memory required to store a structure or cell array depends not only on how much data it holds, but also how it is constructed. For example, a scalar structure array S1 having fields R, G, andB, eachfieldof size 100-by-50, requires one array header to describe the overall structure, and one header to describe each of the three field arrays, making a total of 4 array headers for the entire data structure: S1.R(1:100,1:50) S1.G(1:100,1:50) S1.B(1:100,1:50) On the other hand, a 100-by-50 structure array S2 inwhicheachelementhas scalar fields R, G, andB requires one array header to describe the overall structure, and one array header per field for each of the 5,000 elements of the structure, making a total of 15,001 array headers for the entire data structure: S2(1:100,1:50).R 11-14
Using Memory Efficiently S2(1:100,1:50).G S2(1:100,1:50).B Thus, even though S1 and S2 contain the same amount of data, S1 uses significantly less space in memory. Not only is less memory required, but there is a corresponding speed benefit to using the S1 format as well. Memory Usage Reported By the whos Function. The whos function displays the amount of memory consumed by any variable. For reasons of simplicity, whos reports only the memory used to store the actual data. It does not report storage for the variable itself or the array header. Function Arguments MATLAB handles arguments passed in function calls in a similar way. When you pass a variable to a function, you are actually passing a pointer to the data that the variable represents. As long as the input data is not modified by the function being called, the variable in the calling function and the variable in the called function point to the same location in memory. If the called function modifies the value of the input data, then MATLAB makes a copy of the original array in a new location in memory, updates that copy with the modified value, and points the input variable in the called function to this new array. In the example below, function myfun modifies the value of the array passed into it. MATLAB makes a copy in memory of the array pointed to by A, sets variable X as a pointer to this new array, and then sets one row of X to zero. The array referenced by A remains unchanged: A = magic(500); myfun(A); function myfun(X) X(400,:) = 0; If the calling function needs the modified value of the array it passed to myfun, you will need to return the updated array as an output of the called function, as shown here for variable A: A = magic(500); A = myfun(A); 11-15
- 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 764 and 765: 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 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
- Page 814 and 815: 12 Programming Tips Strings This se
- Page 816 and 817: 12 Programming Tips Function regexp
- Page 818 and 819: 12 Programming Tips are useful when
- Page 820 and 821: 12 Programming Tips File Precedence
- Page 822 and 823: 12 Programming Tips Making Nontoolb
Using Memory Efficiently<br />
S2(1:100,1:50).G<br />
S2(1:100,1:50).B<br />
Thus, even though S1 and S2 contain the same amount of data, S1 uses<br />
significantly less space in memory. Not only is less memory required, but<br />
there is a corresponding speed benefit to using the S1 format as well.<br />
Memory Usage Reported By the whos Function. The whos function<br />
displays the amount of memory consumed by any variable. For reasons of<br />
simplicity, whos reports only the memory used to store the actual data. It does<br />
not report storage for the variable itself or the array header.<br />
Function Arguments<br />
<strong>MATLAB</strong> handles arguments passed in function calls in a similar way. When<br />
you pass a variable to a function, you are actually passing a pointer to the<br />
data that the variable represents. As long as the input data is not modified<br />
by the function being called, the variable in the calling function and the<br />
variable in the called function point to the same location in memory. If the<br />
called function modifies the value of the input data, then <strong>MATLAB</strong> makes<br />
a copy of the original array in a new location in memory, updates that copy<br />
with the modified value, and points the input variable in the called function<br />
to this new array.<br />
In the example below, function myfun modifies the value of the array passed<br />
into it. <strong>MATLAB</strong> makes a copy in memory of the array pointed to by A, sets<br />
variable X as a pointer to this new array, and then sets one row of X to zero.<br />
The array referenced by A remains unchanged:<br />
A = magic(500);<br />
myfun(A);<br />
function myfun(X)<br />
X(400,:) = 0;<br />
If the calling function needs the modified value of the array it passed to<br />
myfun, you will need to return the updated array as an output of the called<br />
function, as shown here for variable A:<br />
A = magic(500);<br />
A = myfun(A);<br />
11-15