Search code examples
matlaboptimizationtiming

Testing speed of algorithms in MatLab by timing execution for loops - not reliable because the for loop positioned first is always faster?


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'?


Solution

  • 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