필터 지우기
필터 지우기

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 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