1

The algorithm repeats the same thing again-and-again. I expected to get the same time in each trial but I got very unexpected times for the four identical trials

enter image description here

in which I expected the curves to be identical but they act totally differently. The reason is probably in the tic/toc precision.

  • What kind of profiling/timing tools should I use in Matlab?
  • What am I doing wrong in the below code? How reliable is the tic/toc profiling?
  • Anyway to guarantee consistent results?

Algorithm

A=[];
for ii=1:25
    tic;
    timerval=tic;
    AlgoCalculatesTheSameThing();    
    tElapsed=toc(timerval);
    A=[A,tElapsed];   
end
Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
hhh
  • 50,788
  • 62
  • 179
  • 282
  • Note that asking for tool recommendations is off topic as per the [help/on-topic] (curious why you didn't know...). You should probably remove that part (or rephrase it to something more appropriate, if possible - not sure it can be though). – Bernhard Barker Dec 13 '13 at 05:49
  • 1
    @Dukeling: IMHO, this is not off topic. One out of four questions involves the phrase "tools", whereas the other three questions are definitely on topic. Also, (and I might be wrong here), the first question is not so bad either. Have a look at [this question](http://stackoverflow.com/questions/855126/what-is-the-best-way-to-profile-javascript-execution?rq=1) for instance. The question goes "...what would be the best tool...", and it has 55 up votes, 0 down votes and 19 favorites. – Stewie Griffin Dec 13 '13 at 07:05
  • That question is 4.5 years old, and a lot has changed since then, so it's not the best example (and if it's not closed yet, it might just mean 5 people hasn't gotten to it yet). I'm not saying the **whole** question is off topic (otherwise I would've voted to close), just that part (and given that it's the *first* question, people could close the question because of it). – Bernhard Barker Dec 13 '13 at 07:12

3 Answers3

7

You should try timeit.

Have a look at this related question:

How to benchmark Matlab processes?

A snippet from Sam Roberts answer to the other question:

It handles many subtle issues related to benchmarking MATLAB code for you, such as:

  • ensuring that JIT compilation is used by wrapping the benchmarked code in a function
  • warming up the code
  • running the code several times and averaging

Have a look at this question for discussion regarding warm up:

Why does Matlab run faster after a script is "warmed up"?

Update:

Since timeit was first submitted at the fileexchange, the source code is available here and can be studied and analyzed (as opposed to most other MATLAB functions). From the header of timeit.m:

%   TIMEIT handles automatically the usual benchmarking procedures of "warming
%   up" F, figuring out how many times to repeat F in a timing loop, etc.
%   TIMEIT also compensates for the estimated time-measurement overhead
%   associated with tic/toc and with calling function handles.  TIMEIT returns
%   the median of several repeated measurements.

You can go through the function step-by-step. The comments are very good and descriptive in my opinion. It is of course possible that Mathworks has changed parts of the code, but the overall functionality is there.

For instance, to account for the time it takes to run tic/toc:

function t = tictocTimeExperiment
% Call tic/toc 100 times and return the average time required.

It is later substracted from the total time.

The following is said regarding number of computations:

function t = roughEstimate(f, num_f_outputs)
%   Return rough estimate of time required for one execution of
%   f().  Basic warmups are done, but no fancy looping, medians,
%   etc.

This rough estimate is used to determine how many times the computations should run.

If you want to change the number of computation times, you can modify the timeit function yourself, as it is available. I would recommend you to save it as my_timeit, or something else, so that you avoid overwriting the built-in version.

Community
  • 1
  • 1
Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
  • "... and including the links in the text ..." - but it's nice to be able to see the question title without having to hover over the link. – Bernhard Barker Dec 13 '13 at 05:51
  • 1
    @Dukeling: Pesonally, I like it when the links are part of the text. IMO, it is easier to read that way. But I'll keep this one the way it is =) – Stewie Griffin Dec 13 '13 at 06:09
  • How can you change the number of computation times in timeit? The term averaging is misleading when it uses median, according to [this](http://www.mathworks.com/matlabcentral/fileexchange/18798-timeit-benchmarking-function). Is there any function that computes as long as the standard deviation in computation times is below some level? Currently, the timeit looks like a blackbox. **[Update]** reading the update :) – hhh Dec 13 '13 at 10:50
  • @hhh: I haven't studied the function in-depth, so I can't answer that question. Please look through the function, the comments are quite informative. – Stewie Griffin Dec 13 '13 at 10:55
  • 1
    Good suggestion! I did my own version of `timeit` [here](http://stackoverflow.com/a/20565521/164148) where I changed the outer loops from 11 to 50 and I was able to get far better results than let say with the tic/toc 500 times or the original `timeit`, yet taking a little bit more computation time. The warming up is clearly very important :) – hhh Dec 13 '13 at 11:35
3

Qualitatively there are large differences between the same runs. I did the same four trials as in the question and tested them with the methods suggested so far and I created my own version of the timeit timeitH because the timeit has too large standard deviation between different trials. The timeitH returns far more robust results to other methods because it warm ups the code similarly to the timeit and then it has increased the amount of outer loops in the original timeit from 11 to 50.

The below has the four trials done with the three different methods. The closer the curves are to each other, the better.

TimeitH: results pretty good!

enter image description here

Some observations.

  1. timeit: result smoothed but bumps
  2. tic/toc: easy to adjust for larger cases to get the standard deviation smaller in computation times but no warming up
  3. timeitH: download the code and change 60th line to num_outer_iterations = 50; to get smoother results

In summarum

I think the timeitH is the best candidate here, yet only tested in evaluating sparse polynomials. The timeit and tic/toc like 500 times do not result into robust results.

Timeit

enter image description here

500 trials and average with tic/toc

enter image description here

Algorithm for the 500 trials with tic/toc

for ii=1:25
    numTrials = 500;
    tic;
    for ii=1:numTrials
        AlgoCalculatesTheSameThing();   
    end
    tTotal = toc;  
    tElapsed = tTotal/numTrials;
    A=[A,tElapsed];   
end
Community
  • 1
  • 1
hhh
  • 50,788
  • 62
  • 179
  • 282
  • @Masi Aliasing and smoothing are totally two different concepts: smoothing is defined in terms of the derivatives while aliasing requires at least two functions. If you have two lines, you will see third line -- look a bridge metal piles or anything like that. Movre effect is nothing special, it and the aliasing has nothing to do with this question. ONly if you have two lines very closeby like in in peak but it is pretty irrelavant here. Beautiful smooth functions, at least I cannot see any blurring lines due to mixing of lines. – hhh Dec 15 '13 at 22:09
2

Is the time for AlgoCalculatesTheSameThing() relatively short (fractions of sec or a few sec) or long (multi-minutes or hours)? If the former I would suggest doing it more like this: move your timing functions outside your loop, then compute averages:

A=[];
numTrials = 25; 
tic;
for ii=1:numTrials
    AlgoCalculatesTheSameThing();     
end
tTotal = toc;
tAvg = tTotal/numTrials;

If the event is short enough (fraction of sec) then you should also increase the value of numTrials to 100s or even 1000s.

You have to consider that with any timing function there will be error bars (like in any other measurement). If the event your timing is short enough, the uncertainties in your measurement can be relatively big, keeping in mind that the resolution of tic and toc also has some finite value.

More discussion on the accuracy of tic and toc can be found here.

You need to work out these uncertainties for your specific application, so do experiments: perform averages over a number of trials and then compute the standard deviation to get a sense of the "scatter" or uncertainly in your results.

Bruce Dean
  • 2,798
  • 2
  • 18
  • 30