1

I am trying to determine the optimum way to perform a particular task in MatLab so I created two for loops and set tic and toc before and after each of them:

mMax = 5000;

tic
% Approach 1
for m=1:mMax
    result_1 = ...;
end
toc


tic
% Approach 2
for m=1:mMax
    result_2 = ...;
end
toc

After running the code multiple times, initially it seemed that Approach 1 yielded better results and was about three times faster than Approach 2.

However I then moved the Approach 1 loop after the Appraoch 2 loop. This time it seemed that Approach 2 is a couple of times faster than Approach 1.

So I'm guessing that whatever resources are consumed during the first for loop are impacting the resources available for the second loop?

How can I reliably test the performance of these approaches to see which is the fastest. Is it sufficient to just create a pause in between them? Is there a way to 'flush' MatLab so that the approaches both have a 'level playing field'?

1
  • 1
    Did you try "Profiling" ? Commented Apr 20, 2017 at 9:56

1 Answer 1

1

Without seeing your full code I would suggest the following: create one function for each approach

Something like:

myTest.m

function myTest()
   mMax = 5000;
   tic;
   myApproachA_test(mMax);
   toc;

   tic;
   myApproachB_test(mMax);
   toc;
end

myApproachA_test.m and myApproachB_test.m like:

function myApproachA_test(nTrials)
   for m=1:nTrials
       result_1 = ...;
   end
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.