필터 지우기
필터 지우기

Time Comparison and Tic Toc

조회 수: 3 (최근 30일)
Matt
Matt 2012년 11월 2일
I am having a little trouble using the tic toc function. (For background, I wanted to visualize the difference in calculation times between vectorized equations and for-loops, make a function, called timecompare that takes in a function handle ,fh, and a vector, x, at which to evaluate my function.)
My function timecompare should evaluate my function handle in two ways: (1) using a for-loop to evaluate fh at each element of x, and (2) by simply inputting the whole vector into fh. The structure is function is:
two inputs:
1) A function handle, fh.
2) An array of values, x, at which to evaluate the function handle.
three outputs:
1) time1, the total time it takes to evaluate fh at each value of x using a for-loop.
2) time2, the total time it takes to evaluate fh at each value of x by inputting the whole array into your function.
3) flag, which has a value of 1 if time1 > time2, or a value of 2 if time2 > time1.
Your function header should be:
function [time1, time2] = timecompare(fh,x)
tic
for ii = 1:length(x)
feval(fh,x(ii))
end
time1 = toc;
tic
feval(fh,x)
end
plot(fh)
clearly I am doing something in my code and it’s probably because I am not too familiar with tic toc. How should I go about fixing my code?

답변 (1개)

Arthur
Arthur 2012년 11월 2일
편집: Arthur 2012년 11월 2일
With tic toc you measure the time elapsed between the 'tic' and 'toc' command. So for instance
tic
%you code here
elapsedTime = toc
I guess your code should be something like this:
function [time1, time2] = timecompare(fh,x)
tic
for ii = 1:length(x)
feval(fh,x(ii))
end
time1 = toc;
tic
feval(fh,x)
time2 = toc;
  댓글 수: 3
John Petersen
John Petersen 2012년 11월 2일
You got the error because you added another argument that you haven't included inside your function. Where did "flag" come from?
Arthur
Arthur 2012년 11월 3일
Ah yes my answer didn't include the flag. I was hoping you could include that yourself. A simple if statement.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by