Matlab JIT and execution time

8 次查看(过去 30 天)
Sébastien
Sébastien 2015-5-1
评论: Jan 2015-5-3
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

回答(1 个)

Jan
Jan 2015-5-2
编辑:Jan 2015-5-2
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 个评论
Sébastien
Sébastien 2015-5-3
Then one should be able to selectively turn on/off JIT related warnings. The thing is I wish to write optimized Matlab code and do not want to have to profile each single line of code I am typing... Is there an updated and extensive guidelines document on how to make the best use of the JIT?
To come back to my initial question: In the concrete example I sent, what is exactly happening internally that explains such as huge difference?
Jan
Jan 2015-5-3
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by