Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
Steven Deng
Steven Deng am 13 Okt. 2022
Kommentiert: Star Strider am 13 Okt. 2022
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

Akzeptierte Antwort

Star Strider
Star Strider am 13 Okt. 2022
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 Kommentare
Steven Deng
Steven Deng am 13 Okt. 2022
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 am 13 Okt. 2022
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!
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by