MATLAB Answers


computation time calculation

Asked by ramya raj on 23 Oct 2011
Latest activity Edited by Walter Roberson
on 9 Feb 2016
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


Sign in to comment.

2 Answers

Answer by 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:
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.


Hi, Jan Simon
may I know the units (sec/min etc) of the following:
TIC TOC: 1.00754
CPUTIME: 0.0468003
CLOCK: 1.006
on 9 Feb 2016
As explained in the documentation of cputime, tic and etime, the units are seconds.

Sign in to comment.

Answer by Walter Roberson
on 23 Oct 2011
Edited by 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.


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
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.

Sign in to comment.