I recently talked about the Copy on write mechanism. It ensures that, when you copy a variable, memory is handled in an optimal way. I underlined that when you use subfunction, this can be quite useful as you in effect only pass a reference to the data, This is valid until you modify the data in your subfunction. This can come handy especially if you deal with large datasets.
There is one important exception to this.
In many cases, you want to modify a variable and you don’t actually care to keep its previous value. You really don’t want Matlab to make internal copy of all the intermediate state of your variable. Ideally, you would like the modification to be directly where the variable is located, element by element. This is possible and it is called In-place computation.
Suppose you do the following :
I chose x as big 3D matrix to push your computer a little. This might take a couple seconds. This is standard calculation. In the end, you get 2 matrices, x and y that are quite large in memory. On my machine, Matlab spend about 2 seconds on line 1 and 1.2 seconds on line 2.
Now please try this code :
The only difference is that you ask to send the result to x itself. You might think that Matlab is going internaly to make an intermediate copy of x to handle this calculation. Well, since MATLAB 2006b, no, it actually directly modify the variable in its original memory location. On this, the profiler tells me that Matlab now spend 0.6 seconds on line 2. This line is two times faster because we don’t have to declare some memory again.
This mechanism is valid with subfunction as well. Please note there are some rules to its usage.
I am sure lots of you have actually been using this mechanisms without actually knowing that Matlab does this in the background (At some point I was too).
Why is this an exception to the Copy On Write? Well because if you send x to both the input and the output of the subfunction, Matlab will keep working with a reference even if you actually modify your variable. One could correct “Copy On Write” with “Copy On Write Unless To Itself”.