0
votes

how can I compare the execution time of two different methods simultaneously in MatLab? actually I used "tic-toc" but I'm not sure if I did it in a correct way or not. this is how I do it:

clc;
clear all;
A=rand(10);
B=rand(50;
tID1=tic;
y1=function1(A,B);
t1=toc(tID1);
tID2=tic;
y2=function2(A,B);
t2=toc(tID2);
2
Looks fine. See also timeit - Luis Mendo
As @LuisMendo suggests, you should really look into timeit. There are a lot of subtleties in timing MATLAB code, which tic and toc do not deal with. timeit handles things properly. In recent versions, timeit is built in to MATLAB, otherwise download by following Luis' link. - Sam Roberts

2 Answers

2
votes

Use matlab's built in profiler for better understanding of your code's run-time bottlenecks.

profile clear; %// reset profiler's history
profile on;    % start recording
y1 = function1( A, B ); % your code here...
y2 = function2( A, B );
profile off;   % stop recording
profile viewer; % visualize the results.

Enjoy!

0
votes

Yes. That is correct. You don't even need to define tID1 and tID2 the way you are using it. So you can also do:

clc;
clear all;
A=rand(10);
B=rand(50;
tic;
y1=function1(A,B);
t1=toc;
tic;
y2=function2(A,B);
t2=toc;