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

2 次查看(过去 30 天)
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 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