フィルターのクリア

Is there a way to store execution times on a disk?

2 ビュー (過去 30 日間)
Steven Deng
Steven Deng 2022 年 10 月 13 日
コメント済み: Star Strider 2022 年 10 月 13 日
I am coding a program that add integer numbers, from 1 to N, one by one, where N is supposed to be an input.
However, I am also given the task of having the program take each intermediate result, storing them onto a disk, and report on execution times.
The first part is working out well, but I have no clue how to do the second part. Is this task possible for MATLAB to do?
So far, I have:
function fact = addInteger(n)
fact = 0;
for i = 1:n
fact = fact + i;
end
end

採用された回答

Star Strider
Star Strider 2022 年 10 月 13 日
Perhaps tic and toc
s(1) = 0;
t0 = tic;
for k = 2:10
s(k) = s(k-1) + 1;
t_int(k) = toc(t0);
end
Result = [s; t_int]
Result = 2×10
0 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 0 0.0023 0.0027 0.0028 0.0028 0.0034 0.0036 0.0036 0.0036 0.0036
N = 10;
s = zeros(1,N); % Preallocate
t0 = tic;
for k = 2:10
s(k) = s(k-1) + 1;
t_int(k) = toc(t0);
end
Result = [s; t_int]
Result = 2×10
0 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 0 0.0009 0.0010 0.0010 0.0010 0.0012 0.0013 0.0013 0.0013 0.0013
.
  4 件のコメント
Steven Deng
Steven Deng 2022 年 10 月 13 日
I managed to figure it out, I meant something like this:
function fact = addInteger2(n)
s(1) = 0;
t0 = tic;
for k = 2:n+1
s(k) = s(k-1) + (k-1);
t_int(k) = toc(t0);
end
Result = [s; t_int]
end
I wanted to add each consecutive integer, but at the same time, timing the results, so I could produce something like this:
addInteger2(4)
Result =
0 1.0000 3.0000 6.0000 10.0000
0 0.0005 0.0008 0.0009 0.0010
Thanks for your help, really appreciated it!
Star Strider
Star Strider 2022 年 10 月 13 日
My pleasure!
I don’t see how your code differs significantly from mine, though, at least with respect to the tic and toc calls.
If my Answer helped you solve your problem, please Accept it!
.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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