# Steve on Image Processing

## timeit makes it into MATLAB

This is my first blog post with "Published with MATLAB R2013b" at the bottom. The latest MATLAB release shipped earlier in September. And, for the first time in a while, a function that I wrote has made it into MATLAB.

Back in 2008, I spent some time trying to incorporate performance benchmarking lessons I learned from Cleve Moler and Bill McKeeman into a general-purpose function for accurately measuring the "typical" running time of MATLAB functions or expressions. The result was timeit, which I submitted to the File Exchange.

Well, I'm happy to say that timeit has made it into MATLAB in the R2013b release! The MATLAB development team took the code from the File Exchange, made some minor refinements, did some additional testing, wrote some nice doc, and got it into the release. (Thanks, everyone!)

Here are a couple of examples. The first one is just MATLAB, but the second one requires Image Processing Toolbox. (Note that it's helpful to know a little about anonymous functions in order to use timeit.)

How much time does it take to compute sum(A.' .* B, 1), where A is 12000-by-400 and B is 400-by-12000?

A = rand(12000,400);
B = rand(400,12000);
f = @() sum(A.' .* B, 1);
timeit(f)

ans =

0.0397



How much time does it take to dilate the text.png image with a 25-by-25 all-ones structuring element?

bw = imread('text.png');
se = strel(ones(25,25));
g = @() imdilate(bw,se);
timeit(g)

ans =

7.7779e-04



Loren also wrote about timeit recently in her Art of MATLAB blog.

There are other goodies in R2013b that I'll write about soon.

Get the MATLAB code

Published with MATLAB® R2013b

### 4 Responses to “timeit makes it into MATLAB”

1. Sean de Wolski replied on :

I never downloaded TIMEIT from the FEX, but now that it’s in 13b I use it daily.

I wish I’d known how useful it is…

2. Steve Eddins replied on :

3. Image Analyst replied on :

Can you give the advantages of timeit over tic and toc?

4. Steve Eddins replied on :

Image Analyst—timeit uses tic and roc, but timeit takes care of a number of details automatically. For example, it automatically constructs two loops. The inner loop, which is bracketed by calls to tic and toc, repeats the operation until the elapsed time is above a certain threshold (about 10 ms if I remember correctly). The purpose of the inner loop is to minimize the effect of the overhead in calling tic and toc. The purpose of the outer loop is to perform multiple timing experiments, returning the median of the results. The number of inner and outer loop repetitions is determined automatically.

So there’s no reason for you to put a call to timeit inside a loop and then divide by the number of loop iterations, which is what people normally do with tic/toc, because timeit is doing all of that for you.

Steve Eddins is a software development manager in the MATLAB and image processing areas at MathWorks. Steve coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.

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