Timing multiple Matlab functions

5 次查看(过去 30 天)
Hi!
I have two different ways of calculating a factorial of any number, for this program its 150!. I want to time the two functions and compare how much time they require in a plot.
This is my code so far:
clear all, close all, clc
n=150;
t1=zeros(1,n);
t2=zeros(1,n);
A=zeros(1,n);
B=ones(1,n);
f=1;
tic;
for i=1:n
f=f*i;
A(1,i)=f;
t1(i)=toc;
end;
tic;
for i=2:n
B(i)=B(i-1)*i;
t2(i)=toc;
end;
plot(t1,A,t2,B)
I do get a plot with the correct graphs, however I am concerned that the times are not correct since first of all, the first for-loop is faster than the second one and I think it should be around. Also, I have tried changing their places in the program and I get different results when I do.
How can I change this to get Matlab to time these functions accurately? Also, if you have a suggestion I would be very thankful if you could also point out what I am doing wrong.
Thank you!

采纳的回答

Titus Edelhofer
Titus Edelhofer 2016-2-17
Hi,
timing single calls within a program using tic/toc won't work, the resolution will simply be too coarse.
What are you trying to find out? If the second or the first is preferable? Why then not simply run the loop and do the timing after the loop.
Additional suggestions: put your code into a function, it will be more realistic and give better results than scripts.
You might put the loops into individual sub functions and call them with different values of n if you are interested in the dependency on n.
Titus
  1 个评论
Alexander Engman
Alexander Engman 2016-2-17
Thank you for your answer.
Yes, what I want to find out which of the methods is quicker and thus preferable. I want them both to calculate a factorial of an arbitrary number and then compare how much time it took for them to reach the factorial in a plot.
Could you please expand on what you mean by "timing after the loop", perhaps with an example?
Thank you for your suggestion. I have now put my codes into functions and like you said, the results were better but it does not change the result (that B takes less time than A).
PS. When I compared the values of the timing of the "recursive" factorial (B) with the factorial function in Matlab, I still get the result that B reaches the factorial faster. So could it be that the results of the timing are in fact correct?

请先登录,再进行评论。

更多回答(1 个)

Titus Edelhofer
Titus Edelhofer 2016-2-17
Hi,
here is some code how you can test:
function testfactorial
n = 150;
t1 = timeit(@() fact1(n), 1)
t2 = timeit(@() fact2(n), 1)
function A = fact1(n)
f = 1;
A = zeros(1, n);
for i=1:n
f=f*i;
A(1,i)=f;
end
function B = fact2(n)
B = zeros(1, n);
for i=2:n
B(i)=B(i-1)*i;
end
If you run this a couple of times you will see one time t1 smaller, the other t2. So they are equally good and differences are stochastical ...
Titus
  1 个评论
Alexander Engman
Alexander Engman 2016-2-17
编辑:Alexander Engman 2016-2-17
Yes I see it when I try your code. So I think obviously something is wrong with mine because I am getting much larger differences between the timings. But I can't figure out what is wrong. Do you have any advice on how I can change my code and still use the tic/toc functions but get the right time-values? The reason I still want to use tic/toc is to be able to display it in a plot.
Edit: I just realised the problem. Rookie mistake. I put the tic outside of the for-loop which of course made it take a lot longer than if it were inside the for-loop. So by placing the tic after for, my problems are solved!
Thank you for your help!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by