0
votes

Suppose we have two methods or algorithm. Suppose the code for each one as follow:

Method 1:

for i=0 to 100
 print i
 end for

Method 2:

int x=0
 w = x/2
 print w

What is the best approach to compare the computation time between method 1 & 2? I tried to use Matlab code:

t= cputime; 
 Method 1
 e = cputime-t

But I'm not sure if this is the correct way to compare the performance of those methods or not.

2

2 Answers

3
votes

Use the timeit function, this comes with Matlab from 2013b onward but it is available on the file exchange if you are using an older version. This will correctly "warm up" the function for you before timing it and also outs your function in a loop internally and it reports the median time over many runs for you.

Otherwise the conventional method is to use tic and toc around a loop containing your function.

0
votes

Please read this for complete info.

n = 1e4; %Higher number results in more accurate comparison
tic;
for t=1:n
  {method1}
end
toc;

tic;
for t=1:n
  {method2}
end
toc;

This will give you time-elapsed each time.