Note that there are some explanatory texts on larger screens.

plurals
  1. POtiming code in matlab
    primarykey
    data
    text
    <p>I have written down a function in 4 different ways and I want to time it . </p> <p>Up-to now I have been doing this thing : </p> <pre><code>tic %//function 1 toc tic %//function 2 toc tic %//function 3 toc tic %//function 4 toc </code></pre> <p>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? </p> <p>Also I read somewhere that the time printed is the elapsed “wall clock” time – so it will be affected by whatever else my computer is doing whilst the MATLAB program was running.</p> <p>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?</p> <p>I have also consulted the sites : <a href="http://blogs.mathworks.com/pick/2008/02/12/timing-code-in-matlab/" rel="noreferrer">Timing code in MATLAB</a> and <a href="http://blogs.mathworks.com/pick/2006/10/19/profiler-to-find-code-bottlenecks/" rel="noreferrer">Profiler to find code bottlenecks</a>.</p> <p>Please suggest how to do this many times in a loop. Thanks in advance. </p> <p><em><strong>EDIT: 23rd Sept 2013: By following everyone's suggestions I did this : My functions are defined as one, two, three &amp; four.</em></strong> </p> <pre><code>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 </code></pre> <p>I then ran this program and got this times : </p> <pre><code>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 </code></pre> <p><em>The times I got are :</em> </p> <pre><code>%//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. </code></pre> <p>Is my timing code wrong ? I mean technically I should be getting the fastest times for the function three &amp; four always. For different values of x , I am getting different results. </p> <p>Please guys could you suggest me a better timing measurement approach ?? Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload