s = timeit(f)f is a function handle, and s is the time, in seconds, required to call that function handle. timeit calls f with no input arguments. To use timeit successfully, it's very helpful to know how to make anonymous function handles. Here are a couple of examples: 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.1125How 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 = 0.0037The function timeit is designed to handle three key timing issues automatically:
- "Warming up" the function to be timed, in order to eliminate one-time effects related to file systems, memory systems, M-file parsing and optimization, etc.
- Adjusting to the granularity of the timer functions tic and toc. timeit does this by calling f in an "inner loop," with the number of inner loop repetitions set so that each call to tic / toc measures a time interval of at least about 0.01 seconds. The appropriate number of inner loop repetitions is automatically determined.
- Running an "outer loop" that calls and times the inner loop in a set of repeated trials. The median time for the trials is then used to compute the time estimate.
Get the MATLAB code Published with MATLAB® 7.5
Comments are closed.
8 CommentsOldest to Newest
Hi Steve, I write to have a disscussion with you on MATLAB function imtransform.
My question is how to get back to the original image after some kind of transformation, for example affine transform. The following codes can illustrate my question:
I = imread(‘cameraman.tif’);
I = im2double(I);
delta = pi/6;
t = [ 2*cos(delta), sin(delta), 0
-sin(delta), 2*cos(delta), 0
3, 4, 1];
tform = maketform(‘projective’,t);
[II,xdata,ydata] = imtransform(I,tform);
revt = inv(t);
revtform = maketform(‘projective’,revt);
III = imtransform(II,revtform,’UData’,xdata,’VData’,ydata,’XData’,[1,256],’YData’,[1,256]);
Hower, the orignal image I and III are different not only in theirwidth and height but also in their gray value.
Would you like to open a new topic on this problem?
Feelfree—Use the ‘Size’ parameter in your second call to imtransform.
Thanks Steve, and I do as you suggest, and in this case the output image is of the same size with the original one, but their gray value still have difference though not much. The average gray value difference is about 2 gray level for uint8 type image. sum(abs(III(:)-I(:)))/256/256*256. Then how can I deal with this problem? Thanks. Hope I did not throw a spanner in your current topic.
Feelfree—There’s no real problem here. You’re just seeing the affects of interpolation. Try this:
imshow(I - III, ) impixelinfo
and then move the mouse over the image. You’ll see that the difference is zero just about everywhere except for along edges, which is what you should expect because of the interpolation approximation.
If whatever you are doing is sensitive to this, then you’ll need to rethink your approach.
Thanks Steve, i see.
i want to know that how i can use ‘timeit’ for parallel processing.In linux we do ‘fork’ which makes parent and child and processor fastly switch between parent and child to service them.How can we do this thing in matlab.i have to make two functions and processor should switch between these two functions so that it seem that we are doing parallel processing of two functions.
Muhammad—timeit doesn’t have anything to do with parallel processing. Many MATLAB functions are already multithreaded and so will automatically use multiple processor cores. If you are interested in explicit parallel programming, you might be interested in the Parallel Computing Toolbox.
I m a little bit late to ask this on your Blog.
So, im using an ActiveX Control of a DAQ Board.
I need to generate a code in a loop which have to be determined to work in 50 ms, the problem is that this loop have many Methods and Properties.
Can you tell me which timing I must use for it ?