To get the time it takes a program to execute, I set date_1 = datetime('now") at the beginning of a program, and date_2 = datetime('now) at the end of the program. Then Elapsed_time = date_2 - date_1. I would like to print the elapsed time using the fprintf command, and I would like it to be formatted to show minutes and seconds. How do I do that? Could you provide an example?

 채택된 답변

Star Strider
Star Strider 2022년 2월 28일

2 개 추천

Try this —
date_1 = datetime('now');
for k = 1:1E+8
rand;
end
date_2 = datetime('now');
Elapsed_time = date_2 - date_1;
Elapsed_time.Format = 'mm:ss.SSS';
fprintf('Elapsed time = %s', Elapsed_time)
Elapsed time = 00:07.641
I added the milliseconds simply for the demonstration.
.

댓글 수: 4

JDA
JDA 2022년 2월 28일
That worked! Thanks
Star Strider
Star Strider 2022년 2월 28일
As always, my pleasure!
JDA
JDA 2022년 3월 1일
Great answer! That solved my problem. Thanks.
Star Strider
Star Strider 2022년 3월 1일
Thank you!
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2022년 2월 28일

1 개 추천

date_2 = datetime('now');
date_1 = date_2 - rand()^2 %days
date_1 = datetime
28-Feb-2022 14:32:31
Elapsed_time = date_2 - date_1
Elapsed_time = duration
05:51:40
Elapsed_time.Format = 'mm:ss'
Elapsed_time = duration
351:40
fprintf('elapsed time was: %s\n', Elapsed_time)
elapsed time was: 351:40

카테고리

도움말 센터File Exchange에서 Time Series Objects에 대해 자세히 알아보기

제품

릴리스

R2019b

질문:

JDA
2022년 2월 28일

댓글:

2022년 3월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by