computation time calculation

16 次查看(过去 30 天)
ramya raj
ramya raj 2011-10-23
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

回答(2 个)

Jan
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.
Another method is using the timer of the OS, e.g. for Windows: FEX: High accuracy timer.
  4 个评论
Arghavan Kassraie
Arghavan Kassraie 2022-3-27
is the code above supposed to be at the end of the function?
Walter Roberson
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
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 个评论
ramya raj
ramya raj 2011-10-23
then want is the command for calculating the computation time
whether i can use clock if i can use how to obtain the time by using clock
Walter Roberson
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by