computation time calculation
13 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Jan
on 23 Oct 2011
"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 Comments
Walter Roberson
on 29 Mar 2022
No, the code @Jan posted is the complete demonstration code to illustate the difference between tic/toc and cputime()
Walter Roberson
on 23 Oct 2011
Edited: John Kelly
on 27 May 2014
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 Comments
Walter Roberson
on 23 Oct 2011
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.
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!