MATLAB's R2015b new JIT experiences a severe degradation in speed in the following example, but the documentation says it was significantly improved. Why would the following simple code run so much slower?

조회 수: 2 (최근 30일)
When I run the following code:
I = 1000;
J = 1000;
K = 1000;
A = rand(I,K);
B = rand(K,J);
C = zeros(I,J);
tic
for i=1:I
for j=1:J
for k=1:K
C(i,j) = C(i,j)+A(i,k)*B(k,j);
end
end
end
toc
it takes 7.7 seconds on R2015a and 26.1 seconds on R2015b, which is over a 3 times slowdown. I've emailed support, but they cannot determine why it runs slower, only that the new JIT should speed up code on average. However, this code seems very simple and I'd like to better understand when the new JIT will increase performance vs degrade performance. Thanks for any help.

채택된 답변

Philip Borghesani
Philip Borghesani 2015년 9월 4일
편집: Philip Borghesani 2015년 9월 4일
The old (PreR2015b) JIT was highly optimized for a few simple code situations and fell back to the interpreter when it could do nothing. The new JIT runs all code but is not quite up to the same levels of performance for some simple constructs that the previous JIT could handle well. You happened to find a sweet spot for the old JIT.
The standard rules for making your code run fast still apply. R2015b runs your code faster if it is a function than if it is a script and a simple vectorization gets back most of the performance difference:
for i=1:I
for j=1:J
C(i,j)=sum(A(i,:)'.*B(:,j));
end
end
One of the problems with the previous JIT was it's instability. Try adding rng('default') to the script before creating the test variables (line 4) and look what happens to the time.
I bet somebody can figure out how to do this better, however, this code is much faster then the original and shows what can be done with a bit more involved vectorization:
tic
for j=1:J
C(:,j)=sum(bsxfun(@times,A,B(:,j)'),2);
end
toc
On my machine, which seems quite a bit slower then yours, this takes 3.8 seconds in R2015a and 3 seconds in R2015b.
  댓글 수: 1
Philip
Philip 2015년 9월 4일
Thanks for your answer. I don't need this specific code to run faster (it's just a naive loop to calculate C=A*B, which takes about 0.05 seconds on my machine). I was just using it as a benchmark for testing the new JIT and was curious about the results I saw. Thanks again for the insights.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by