
69Standard Performance-Tuning Techniques
elseif round(rand)
vec = [0,0,0];
else
vec = [1,1,1];
end
value(idx) = sum(vec);
end
⇨ Elapsed time is 2.459548 seconds.
% Simplified branch code - 1.9x faster
value = zeros(1,1e6);
for idx = 1 : 1e6
if rand >= 0.5
value(idx) = sum(rand([1,3]));
elseif rand < 0.5 % else value=0: unchanged from initialization
value(idx) = 3;
end
end
⇨ Elapsed time is 1.307274 seconds.
3.1.5 Unroll Simple Loops
A technique that is often mentioned in performance-tuning literature is loop unrolling (also
called loop unwinding).
127
The idea is to replace a loop with repeated sets of the loop con