very different results R2015a and R2015B simple for loop

1 view (last 30 days)
I have a simple piece of code that computes pi using monte carlo method (it's for a matlab shortcourse I'm teaching)
function piresult = compute_pi_for_rand_inside(N)
tic;
total_inside=0;
for i=1:N
xcoord=rand(1);
ycoord=rand(1);
if (xcoord^2 + ycoord^2 <= 1)
total_inside=total_inside+1;
end
end
piresult = 4*total_inside/N;
toc
fprintf('The computed value of pi is %8.7f.\n',piresult);
end
I call this function with N=100000000
on R2015a I get: "Elapsed time is 10.528362 seconds."
However, using R2015b I get: "Elapsed time is 344.462033 seconds."
Why is there such a huge difference in running time? All hardware conditions are exactly the same.

Accepted Answer

Walter Roberson
Walter Roberson on 22 Feb 2016
The Just-In-Time engine was rebuilt for R2015b; see http://www.mathworks.com/products/matlab/matlab-execution-engine/
By the way, your code could be vectorized considerably.
  2 Comments
Marinus
Marinus on 23 Feb 2016
The issue is that R2015a is MUCH faster than R205b (i.e. the older version is 34 times as fast as the newer version). That should not be the case and, in my opinion, doesn't make any sense.
I realize I can vectorize this using the Matlab operator
pi = 4*(sum((x.^2+y.^2)<=1))/N
but that is not the point here and for short course I'm teaching.
Walter Roberson
Walter Roberson on 23 Feb 2016
As indicated in that link, there can be some patterns that are made slower. I would not expect anything as bad as you see, so I suggest you open a support case about it.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!