What happen at my place, stays at my place

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 :

x=rand(1000,1000,100);
y=x+2;

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 :

x=rand(1000,1000,50);
x=x+2;

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”.

This entry was posted in Advanced, Optimizing your code. Bookmark the permalink.

One Response to What happen at my place, stays at my place

  1. Bjorn says:

    On my computer, the excecution time for example 2 is faster because the array is smaller (50 instead of 100 for the 3rd dimension). Adjusting for this, the execution time is very close for both examples; at least on my computer.

Leave a Reply

Your email address will not be published. Required fields are marked *