I have written down a function in 4 different ways and I want to time it .
Up-to now I have been doing this thing :
tic %//function 1 toc tic %//function 2 toc tic %//function 3 toc tic %//function 4 toc
But now I want to compute the timing data for each function for (say 100 times) each and then compute the average time spent on each function. How can I do so?
So is there a better way of doing it ?? I have heard there is a MATLAB built in code-profiler with the command "profile on". Please can anyone suggest me the way in which I can use it?
I have also consulted the sites : Timing code in MATLAB and Profiler to find code bottlenecks.
Please suggest how to do this many times in a loop. Thanks in advance.
EDIT: 23rd Sept 2013: By following everyone's suggestions I did this : My functions are defined as one, two, three & four.
function [] = one(x) I = imread('coins.png'); I = double(I); I = imresize(I,[x x]); sig=.8; % scale parameter in Gaussian kernel G=fspecial('gaussian',15,sig); % Caussian kernel Img_smooth=conv2(I,G,'same'); % smooth image by Gaussiin convolution [Ix,Iy]=gradient(Img_smooth); f=Ix.^2+Iy.^2; g=1./(1+f); % edge indicator function. end function [] = two(x) I = imread('coins.png'); I = double(I); I = imresize(I,[x x]); sig=.8; % scale parameter in Gaussian kernel G=fspecial('gaussian',15,sig); % Caussian kernel Img_smooth=imfilter(I,G,'conv'); % smooth image by Gaussiin convolution [Ix,Iy]=gradient(Img_smooth); f=Ix.^2+Iy.^2; g=1./(1+f); % edge indicator function. end function [] = three(x) I = imread('coins.png'); I = double(I); I = imresize(I,[x x]); sig=.8; % scale parameter in Gaussian kernel G=fspecial('gaussian',15,sig); % Caussian kernel %//Trying to use 1D convolution instead of using 2D convolution [U,S,V] = svd(G); K1 = U(:,1) * sqrt(S(1,1)); K2 = V(:,1)' * sqrt(S(1,1)); KI1 = imfilter(I,K1,'conv'); Img_smooth = imfilter(KI1,K2,'conv'); % smooth image by Gaussiin convolution [Ix,Iy]=gradient(Img_smooth); f=Ix.^2+Iy.^2; g=1./(1+f); % edge indicator function. end function [] = four(x) I = imread('coins.png'); I = double(I); I = imresize(I,[x x]); sig=.8; % scale parameter in Gaussian kernel G=fspecial('gaussian',15,sig); % Caussian kernel %//Trying to use 1D convolution instead of using 2D convolution [U,S,V] = svd(G); K1 = U(:,1) * sqrt(S(1,1)); K2 = V(:,1)' * sqrt(S(1,1)); KI1 = imfilter(I,K1,'conv'); Img_smooth=conv2(K1,K2,I,'same'); % smooth image by Gaussiin convolution [Ix,Iy]=gradient(Img_smooth); f=Ix.^2+Iy.^2; g=1./(1+f); % edge indicator function. end
I then ran this program and got this times :
clc;clear all;close all; x=64;N=100; tic for i=1:N one(x); end toc tic for i=1:N two(x); end toc tic for i=1:N three(x); end toc tic for i=1:N four(x); end toc
The times I got are :
%//x=64;N=100; Elapsed time is 0.898583 seconds. Elapsed time is 0.983132 seconds. Elapsed time is 0.921140 seconds. Elapsed time is 0.811144 seconds. %//x=128;N=100; Elapsed time is 0.990136 seconds. Elapsed time is 1.044167 seconds. Elapsed time is 0.999153 seconds. Elapsed time is 1.005903 seconds. %//x=256;N=100 Elapsed time is 1.495068 seconds. Elapsed time is 1.416523 seconds. Elapsed time is 1.462653 seconds. Elapsed time is 1.605767 seconds. %//x=1024;N=100 Elapsed time is 16.681720 seconds. Elapsed time is 14.857957 seconds. Elapsed time is 15.580161 seconds. Elapsed time is 19.140707 seconds.
Is my timing code wrong ? I mean technically I should be getting the fastest times for the function three & four always. For different values of x , I am getting different results.
Please guys could you suggest me a better timing measurement approach ?? Thanks in advance!