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?

1 次查看(过去 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
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