11 February, 2014

# Extended time measurement in MATLAB

When developing in MATLAB, it could be crucial to measure execution time of the whole program and the sub-functions. This helps us to identify the bottlenecks in our algorithm and improve code performance.

Each computer has a high-precision internal timer, which lets us to measure elapsed time of microsecond accuracy. In MATLAB tic and toc are functions for interfacing this timer: they can be used in to ways.

## Stopwatch mode

This mode can be imagined as a simple stopwatch: tic (re)starts the time measurement while toc measures the elapsed time from the last call of tic. In addition we can save the result to a variable. See this simple example below:

tic                   % start stopwatchtic                   % restart stopwatchM = magic(20);        % a magic square
toc                   % print the elapsed timeN = magic(20);        % a magic squareelapsedTime = toc     % save the elapsed time

The output is:

Elapsed time is 0.001544 seconds.elapsedTime =   0.0019290

## Timestamp mode

To understand how it really works, use timestamp to note the current value of the internal high-precision timer. In this mode we can save timestamps and calculate the difference between them. To save the current timestamp, simply give the value of tic to a variable:

timeStamp = tic
timeStamp = tic
timeStamp = tic


The output is:

timeStamp =   1392109925334548
timeStamp =   1392109925334627
timeStamp =   1392109925334638


The values are slightly increasing. To measure the elapsed time in this mode, simply give a timestamp parameter to toc: the calculated interval will be the difference between the current and the given timestamps. See the example:

startM = tic;               % save the current timestamp
M = magic(20);              % a magic square
startN = tic;               % save the current timestamp
N = magic(20);              % a magic square
toc(startM)                 % print the elapsed time from startM
elapsedTime = toc(startN)   % save the elapsed time from startN


The output is:

Elapsed time is 0.0015521 seconds.
elapsedTime =   0.00198708

This method gives us more flexibility when measuring time intervals.

Please take care when using stopwatch and timestamp modes together, since they can not be mixed. Calling toc without any input parameter must be preceeded by calling tic without giving its value to any variable, just as in stopwatch mode.

## Average runtime measurement

It is not enough to measure the runtime of a function only once, because some warm-up time is needed to load the code into the memory. In addition the runtime can be dependant of the current processes on the computer too. To measure a more valid value, we shall run the function multiple times and calculate an average, as the following code example shows:

runTime = 0;                  % variable to store runtime
N = 100;                      % average of 100 runs
for run = 1 : N
tic;                        % start stopwatch
M = magic(20);              % generate a magic square
runTime = runTime + toc;    % increase runtime
end
runTime = runTime / N         % calculate average runtime


This way we will get more accurate results.