# Matlab JIT and execution time

10 views (last 30 days)
Sébastien on 1 May 2015
Commented: Jan on 3 May 2015
Hi all,
I am aware of Matlab JIT, and I am happy to see how fast Matlab is getting but I am puzzled by the behavior of the optimizer for this simple script (tested under Matlab R2012a):
tic();
a = 2;
for i = 1:10000000
a = a*1.00000001;
end
toc();
disp(a);
a = ones(10000000,1);
Execution time: 1.121 s (as is) Execution time: 0.096 s (only removing the last line)
I understand that re-defining a variable is somehow bad practice but then a warning should be displayed. Many people are surely making such mistake and will keep on complaining about loop execution time. Can somebody explain what is exactly happening there?
Best, Sébastien

Jan on 2 May 2015
Edited: Jan on 2 May 2015
A good question! Compare these functions:
function a = test1
toc;
a = 2;
for i = 1:10000000
a = a*1.00000001;
end
toc();
disp(a);
a = 1:10; % Simpler than: ones(10000000,1)
function c = test2 % differs: a->c
toc;
a = 2;
for i = 1:10000000
a = a*1.00000001;
end
toc();
disp(a);
c = 1:10; % differs: a->c
test1;
test2;
>> Elapsed time is 2.067935 seconds.
>> Elapsed time is 0.062183 seconds.
(Matlab 2011b/64, Win7, Core2Duo)
To my surprise the JIT acceleration is not impeded by the redefinition:
a = uint8(1:10)
TMW stated, that warnings or hints concerning the JIT are counter-productive, because they would encourage programmers to adjust their code to the JIT, but it is the goal to adjust the JIT to the programmers. This sounds like a fair idea, but in this case a small hint would be such useful.
When the JIT was included in Matlab 6.5 you could get hints for each line of code concerning the JIT. But this feature disappeared later. One of the hints was not to change the class of a variable - and as you see in this example, this hint is outdated and code optimized for the JIT of Matlab 6.5 does not run fast on modern versions. Therefore I understand TMWs decision not to tell the users too many details.
##### 2 CommentsShowHide 1 older comment
Jan on 3 May 2015
JIT warnings would require a distinction of the Matlab version, so you'd need JIT R2011a messages, JIT R2011b messages, JITR2012a messages and so on.
The explanation is trivial: When the variable "a" is redifined later, the JIT does not accelerate the loop. It simply does not recognize reliably, that the loop can be accelerated.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!