Best Practices with Initialization or Prepositioning - MATLAB

My question does not explicitly depend on one piece of code, but is more conceptual.

Unlike some programming languages, MATLAB does not require variables to be explicitly initialized before using them. For example, it is perfectly acceptable to define "myVector" halfway through the script file:

myVector = vectorA .* vectorB

      

My question is, is it faster to initialize variables (like "myVector" above) to zero and then assign values ​​to them, or keep the initialization throughout the entire program?

Here's a direct comparison of what I'm talking about:

Initialization in everything:

varA = 8;
varB = 2;

varC = varA - varB;
varD = varC * varB;

      

Initialization at startup:

varA = 8;
varB = 2;
varC = 0;
varD = 0;

varC = varA - varB;
varD = varC * varB;

      

On the one hand, it seems that there are a few extra lines without extra lines. On the other hand, however, it is a little smart that it would be faster to allocate all memory for a program at the same time, rather than spread across execution time.

Does anyone know a little?

+3


source to share


2 answers


Copy and paste the code Initializing at start:

into the MATLAB editor window and you will receive this warning, which looks like this:

enter image description here

And if you go in Details

, you would read this -

Explanation 
The code does not appear to use the assignment to the indicated variable. This situation occurs when any of the following are true:
Another assignment overwrites the value of the variable before an operation uses it.
The specified argument value contains a typographical error, causing it to appear unused.
The code does not use all values returned by a function call...

      

In our case, the reason for this warning is The code does not use all values

. So this makes it clear that initialization / preallocation won't help in this case.




When should we pre-highlight?

In my experience, pre-allocation helps when you need to index part of it later.

Thus, if you need to index a part varC

to store the results, pre-allocation will help. Hence it makes sense -

varC = zeros(...)
varD = zeros(...)
varC(k,:) = varA - varB;
varD(k,:) = varC * varB;

      

Again, when indexing, if you go out of size varC

, MATLAB will waste time trying to allocate more memory space for it, so it will slow things down a bit. So, preallocate the output variables to which you think will be used to store the results. But, if you don't know the size of the results, you get there and have to add the results to the output variable (s) and that will surely slow you down. maximum size

+5


source


Good! I did some tests and here are the results.

This is the code I used for "all" variable assignments:

tic;
a = 1;
b = 2;
c = 3;
d = 4;
e = a - b;
f = e + c;
g = f - a;
h = g * c;
i = h - g;
j = 9 * i;
k = [j i h];
l = any(k);
b2(numel(b2) + 1) = toc

      

Here's the code for assigning "At Start" variables:

tic;
a = 1;
b = 2;
c = 3;
d = 4;
e = 0;
f = 0;
g = 0;
h = 0;
i = 0;
j = 0;
k = 0;
l = 0;
e = a - b;
f = e + c;
g = f - a;
h = g * c;
i = h - g;
j = 9 * i;
k = [j i h];
l = any(k);
b1(numel(b1) + 1) = toc

      

I have saved the time in vectors "b1" and "b2". Each of these only ran with MATLAB and Chrome, and was the only script file open inside MATLAB. Each of them was performed 201 times. Since the first time the program is run, it compiles, I ignored the first value for both (I'm not interested in compilation time).

To find the average, I used

mean(b1(2:201))

      

and



mean(b2(2:201))

      

Results:

"Everywhere": 1.634311562062418e-05 seconds (0.000016343)

At startup: 2.832598989758290e-05 seconds (0.000028326)

Interestingly (or perhaps not, who knows), defining variables only when needed, propagation throughout the program was almost twice as fast.

I don't know if it has something to do with the way MATLAB allocates memory (maybe it just grabs a huge chunk and doesn't need to allocate more every time you define a variable?), Or if the allocation rate is equal so it gets dwarfed extra lines of code.

NOTE. As Divakar points out, mileage can vary when using arrays. My testing should be fair if the size of the variables doesn't change.

tl; dr Setting variables to zero just to change it below

+3


source







All Articles