フィルターのクリア

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 Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by