1

Does Matlab use branch prediction to improve the performance? That is, if we are flattening out if-statements like this:

if a > 0
   b = b + 1
end

...

b = b + (a > 0)

would it speed things up or not?

PS: I do understand that this specific example would probably never speed things up in any language, but imagine something more complex, i.e. with nested if's or several else statements with multiple conditions...

sashkello
  • 17,306
  • 24
  • 81
  • 109
  • 2
    What specific problem is motivating you to ask this ? – Paul R Jun 19 '13 at 06:00
  • 1
    This is a general question and I'd like to have an answer as general as possible :) I just want to know if those optimization principles I use for C++ are applicable here and to what extent. – sashkello Jun 19 '13 at 06:25
  • Sounds like premature optimisation to me - you should measure/profile performance, and only when you have identified a specific bottleneck then look at micro-optimisations such as the above. – Paul R Jun 19 '13 at 08:29
  • 1
    Which version of MATLAB do you want an answer for? I ask that to motivate you to think in terms of programming for long-term maintainability/elegance etc, rather than prematurely optimising for performance. The MATLAB JIT is a moving target across versions, and effort you put in to possible optimizations such as this may be completely wasted in the next version, or even slow things down. MathWorks improve/change the JIT all the time, and deliberately hide the details to discourage you from over-optimizing. – Sam Roberts Jun 19 '13 at 08:47
  • I'm using 2013a version. Well, I think that having the most possible amount of control over your code is always good, because you never know where is the bottleneck. In this particular case, such statement can be in a loop which gets executed 10^6 times - then it does become important and I'd do such a simple swap to save time, even if it is ~5% speed up... It is easy to change and I'd spend 1 minute of extra time to reshape it if it matters even a little bit. It is worth it if code runs for hours and this is the piece taking 90% of all time. – sashkello Jun 19 '13 at 23:25
  • 1
    And guys, there is at least one comment about premature optimization almost on every question about performance. Don't get me wrong, but it's not really constructive. I don't think that pasting heaps of code and explaining why the question is asked would make any good, will just turn it too localized and hard to read. Here I'm not asking to optimize my code, I'm asking about general principles Matlab is using, and having some habits with C++ code writing I want to know if they are any good with Matlab or not, in situations similar to above. Rody's answer suggests it is not that trivial at all. – sashkello Jun 19 '13 at 23:33

1 Answers1

4

Of course, if you have something like

if a > 0
   b = b + <outcome of some super time consuming calculation>
end

...

b = b + (a > 0) * <outcome of some super time consuming calculation>

the first case will win hands down. Which is how you should structure your code, obviously.

But for simpler cases like the one you propose, the only way to find out is to profile.

clc

N = 1e2;


tend = 0;
for ii = 1:N
    tstart = tic;
    b = 0;
    for jj = 1:1e5
        a = randn;
        if a > 0
            b = b + 1;
        end
    end
    tend = tend + toc(tstart);
end
tend/N


tend = 0;
for ii = 1:N
    tstart = tic;
    b = 0;
    for jj = 1:1e5
        a = randn;
        b = b + (a > 0);
    end

    tend = tend + toc(tstart);
end
tend/N

First run:

ans =
    1.000391000000000e-02
ans =
    5.645930000000001e-03

Second run:

ans =
    5.761880000000000e-03
ans =
    5.599670000000002e-03

Third run:

ans =
     6.127599999999998e-03
ans =
     5.800120000000002e-03

and so on.

So yes, there does seem to be a performance penalty due to branching, although we cannot be sure it's actually due to branch prediction quirks or MATLAB's interpreter/JIT doing something weird with if-else constructs...

As always, if it's performance and fine-grained control you want, don't use MATLAB, use C. This sort of thing is simply not what MATLAB is for.

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
  • 2
    Your first statement *'the first case will win hands down'*, is not always true (due to interpretation). How I understand it: ex-ante is true, but if repeated several times, the time consuming calculation is done only once in the second case. – Oleg Jun 19 '13 at 08:04
  • @OlegKomarov: I was assuming of course that the time consuming calculation is implemented in the best possible way, for both cases (e.g., `(a>0) .* `. In that case, there *will* be useless calculations in the result vector (best-case, same as the `if`, worst case, *all are wasted*). Those losses do not outweigh a comparison, of course, which was the point I was trying to make. – Rody Oldenhuis Jun 19 '13 at 11:24
  • Did you try swapping the two code pieces? Sometimes it matters (I don't really know why but it does). – sashkello Jun 19 '13 at 23:20
  • @sashkello: you mean the order in which the loops are executed? Well, my first attempt was with `rand` instead of `randn`, there was no difference then. When I switched to `randn` (something that can indeed become negative, and is impossible to predict by some compiler optimization), differences started appearing. But reversing the order, I didn't try... – Rody Oldenhuis Jun 20 '13 at 08:41