Asked by Amirhossein Moosavi
on 2 Jul 2019

Dear friends,

I would like to know whether there is a difference between following cases. Your responses are appreciated.

Case 1

X = 1;

Y = X + 4;

Z = X + Y;

Case 2

X = 1; Y = X + 4; Z = X + Y;

Other cases that I want to compare are as follows:

Case 3

v1 = k1{t};

nVar = size(v1, 2);

CP = randsample(nVar-1, 2);

Case 4

CP = randsample(size(k1{t}, 2)-1, 2);

Answer by Guillaume
on 2 Jul 2019

Accepted Answer

No, there is no difference between the cases. More importantly, even if there was, you shouldn't care until you've proven that the operations are a bottleneck.

If you haven't proven that the operations are a bottle neck (by using the profiler), then you should care about clarity, and for that reason, you should prefer case 1 over case 2. As for the other two cases, unless nVar and v1 are going to be reused later on, I'd prefer case 4 as there's little point in creating temporary variables that are only used once.

But again, considerations about speed shouldn't even be on your radar at this point.

Sign in to comment.

Answer by Jan
on 2 Jul 2019

Edited by Jan
on 2 Jul 2019

%Case 1

X = 1;

Y = X + 4;

Z = X + Y;

%Case 2

X = 1; Y = X + 4; Z = X + Y;

The documentation of Matlab stated, that the JIT acceleration works only, if you write one statement per line, because it needs the power to re-order the commands. In your case there is no difference, but in general 1 statement per line is preferred and, by the way, easier to read.

The JIT is not fully documented and subject to changes between Matlab versions. But I can imagine, that this feature is still valid.

% Case 3

v1 = k1{t};

nVar = size(v1, 2);

CP = randsample(nVar-1, 2);

% Case 4

CP = randsample(size(k1{t}, 2)-1, 2);

This is a question of taste. While the runtime is identical, another point gets important: The total time for solviong a problem is built by:

T_total = T_planning + T_programming + T_documentation + T_debugging + T_runtime

Optimizing such tiny pieces of code for the runtime can increase the timings for debugging and documenting. So if the code runs a microsecond faster and is called 1 million times, you save a full second. If you have a typo, which is concealed in the dense code of Case 4, but would be found directly in Case 3, you might save 1 hour of debug time.

"Premature optimization" is a bad programming practice. You loose more than you win. See https://en.wikipedia.org/wiki/Anti-pattern

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 7 Comments

## Shameer Parmar (view profile)

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/469835-which-way-of-programming-is-more-efficient-and-faster#comment_720400

## Alex Mcaulley (view profile)

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/469835-which-way-of-programming-is-more-efficient-and-faster#comment_720406

## Guillaume (view profile)

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/469835-which-way-of-programming-is-more-efficient-and-faster#comment_720407

## Shameer Parmar (view profile)

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/469835-which-way-of-programming-is-more-efficient-and-faster#comment_720419

## Alex Mcaulley (view profile)

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/469835-which-way-of-programming-is-more-efficient-and-faster#comment_720420

## Guillaume (view profile)

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/469835-which-way-of-programming-is-more-efficient-and-faster#comment_720430

## Stephen Cobeldick (view profile)

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/469835-which-way-of-programming-is-more-efficient-and-faster#comment_720443

Sign in to comment.