computation time calculation
조회 수: 16 (최근 30일)
이전 댓글 표시
hai
i want to calculate the computation time of a program in matlab
is there any command in matlab to find the computaion time
i have used tic,toc is it correct
please help me i need very urgently
댓글 수: 0
답변 (2개)
Jan
2011년 10월 23일
"Computation time" is not exactly defined. If you e.g. have a code with much hard-disk interaction, does the idle time during waiting for the disk count as computational time or not? While TIC-TOC uses the CLOCK time and considers the idle time, CPUTIME does not - usually. Try this:
tic;
initime = cputime;
time1 = clock;
pause(1.0); % Wait for a second;
fintime = cputime;
elapsed = toc;
time2 = clock;
fprintf('TIC TOC: %g\n', elapsed);
fprintf('CPUTIME: %g\n', fintime - initime);
fprintf('CLOCK: %g\n', etime(time2, time1));
Under 2011b:
TIC TOC: 1.00754
CPUTIME: 0.0468003
CLOCK: 1.006
I addition CPUTIME is a counter with overflow, such that the 2nd call of CPUTIME need not be greater.
댓글 수: 4
Walter Roberson
2022년 3월 29일
No, the code @Jan posted is the complete demonstration code to illustate the difference between tic/toc and cputime()
Walter Roberson
2011년 10월 23일
편집: John Kelly
2014년 5월 27일
You can use cputime but it isn't recommended.
tic and toc do not measure computation time, they measure elapsed time. But cputime() applied on a multi-core system to something that MATLAB knows how to parallelize is difficult to give much meaning to.
댓글 수: 2
Walter Roberson
2011년 10월 23일
One thing I have seen in the past is that people expect to be able to measure the theoretical efficiency of their program by timing a run of the program. That method has a number of theoretical difficulties when applied to real programs on real computers. If your assignment requires determining the efficiency of an algorithm, then timing is *not* the right way to proceed.
There are a number of operations in MATLAB that can be done quite efficiently by calling out to heavily optimized libraries such as BLAS or LAPAC -- libraries which can often use multiple cores or threads even if you do not have the parallel processing toolbox.
There is, however, a fair bit of work involved in organizing the data the way those libraries want it to be represented, and then after the library routines have run, there is work in organizing the results in the way MATLAB can deal with them. Because of these overheads, it is *slower* to call those library routines until the amount of work to be done is "sufficiently large" that the efficiency of the routines gains more than the cost of transferring the data to and from the routines. (This is a general problem that applies to most forms of parallel processing!)
Because of this, if you measure the "cputime" of your algorithm on a smaller data set, you might arrive at one measure of efficiency, but when your data set gets large enough that MATLAB calls the libraries, your efficiency might change quite noticeably.
There are a lot of other reasons why measuring the "cputime" is usually not the right thing to do.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!