Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top