Avoiding details

Matlab used to interpret statements, in newer versions there is a just-in-time accelerator that usually give a large speedup of a messy code. You should still try to avoid details and use the compiled, builtin commands instead, when possible. Test the following code segment and explain your results:

n = 100000;
x = 0.5 - rand(n, 1);
y = 0.5 - rand(n, 1);
loops = 1000;

tic
for j = 1:loops
for k = 1:n
y(k) = 1.2345e-3 * y(k) + 2.2e-3 * x(k) + x(k) * y(k);
end
end
toc

tic
for j = 1:loops
y = 1.2345e-3 * y + 2.2e-3 * x + x .* y;
end
toc
Run the same code without the JIT, i.e. first give the command feature accel off and then run the code. You may want to decrease n to 10000.


Back