File Exchange Pick of the Week

February 12th, 2008

Timing code in MATLAB

It was about a year and a half ago when I posted about using the profiler to find bottlenecks in your MATLAB code [click here]. This week’s video covers two quick and easy ways of timing your code in MATLAB. This is how people would profile their code before the profiler, but it is still a useful set of tools to have.

In response to the survey, I have obtained new audio recording equipment. Let’s hope you like the results.

Video Content

iconFiles.jpgiconPod.jpglazy.jpg

2 Responses to “Timing code in MATLAB”

  1. Tim Davis replied on :

    Sometimes you want to time code that runs for a teeny fraction of a second, and

    tic ;
    dosomething ;
    t = toc

    is not accurate. The profiler will also be inaccurate. (You might want to time this teeny fraction of a second becaus you want to use the dosomething function zillions of times inside a larger application). Here is a trick I like to use

    tic ;
    t = 0 ;
    ntrials = 0 ;
    while (t < 1)
    dosomething ;
    ntrials = ntrials + 1 ;
    t = toc ;
    end
    t = t / ntrials ;

    That way, you run “dosomething” enough times to accumulate a whole second, and then you compute the average time per call to dosomething.

  2. Doug replied on :

    Tim, an excellent suggestion. I would want to make sure that the time for dosomething >> the rest of the code in loop. For most applications it would be.

    Thanks,
    Doug

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Bob, Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

  • oleg: The author has implemented skewness, kurtosis and checks answering appropriately to the critic.
  • Ashok: how to store 10 or more random number in a loop a loop for i = 1:n mean(i) = input(’enter the mean value...
  • Ben: Doug, Thanks for the very helpful videos! Uitables seem like a convenient way to make a customized property...
  • oleg: Allstats has no checks, no comments and could also be improved (talking about prctile implementatio). Not to...
  • Todd: Additional information and a link to download free MATLAB and Simulink LEGO MINDSTORMS NXT code can be found at...
  • Doug: @Leo, Here is the “English version” of that code. “vec = []” makes an empty variable...
  • leo: Dear Doug I have a question in your code ‘October 9th, 2009 at 13:53′ vec = []; vec = [vec val]; I...
  • Shanker Keshavdas: You sir, are a gentleman and a scholar… No really, helped me out a lot. As to what is...
  • Quan Zheng: how can I get a copy of stepspecs.m?
  • Doug: @Lucy I think this is what you seek to move a line with the mouse in MATLAB. http://blogs.math...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.