Skip to Main Content Skip to Search
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Doug’s 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


Doug Hull is an Application Engineer at The MathWorks. A MATLAB user since 1994, he gets paid to live, eat, and breathe MATLAB! This blog is dedicated to promoting the File Exchange by highlighting files and original video content.



  • Tim Davis: Oh, I’m a bit dyslexic. The code has a variable “west” that should be called...
  • Tim Davis: The fix is to replace the max(diff(r)) line with: [ignore b] = sortrows ([diff(r') a(p(r(1:end-1)))']) b =...
  • Tim Davis: Matt, Good point, thanks for catching that. I missed that in the problem statement. It’s easy to...
  • Matt Fig: Tim, There is a problem in your second code. In the case when a zero block is the same size as a non-zero...
  • Moran: Hi Chris, I tried everything I could think of, and have sent several observations about this bug to support,...
  • odessit: Hello, I have compiled standalone application with deploytool. It would work fine, but the error is in...
  • Duane Hanselman: FYI… % strmatch replacements % strmatch is obsolete, strncmp and strcmp are built in % case...
  • Doug: Duane, I just looked, I did not see an m-lint warning against STRMATCH in 2008a. However, I seem to recall what...
  • hashem: Hi Doug, I built a GUI and write a help for it in “chm” (compiled HTML file) format, and I...
  • Tim Davis: I left out the comments in the code, above. Most of the functions should be familiar to most MATLAB users....

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

Related Topics