MATLAB: How accurate is the CPUTIME function in MATLAB 6.5 (R13)? How does MATLAB wrap around

accuracyaroundcputimeMATLABwrap

How accurate is the CPUTIME function in MATLAB 6.5 (R13)? How does MATLAB wrap around?

Best Answer

The value the CPUTIME function returns to the user is a double (which can accurately represent integers in the range
-2^53 to 2^53
including these two values). However, internally, the value is a signed 32 bit integer, which means it starts at 0, counts to 2^31-1, then it wraps around and continues with -2^31, goes past 0 to 2^31-1, wraps around again and so on.
How fast this wraparound happens depends on the number of clock cycles per second. If the machine has 100 clock cycles per second, then divide 2^31-1 / 100, and the resulting number represents how many seconds may elapse until a wraparound occurs. The problem is that the count starts at 0 either when MATLAB starts, or when the operating system starts up, NOT when the function is called the first time. Thus, if MATLAB has been running for a few days, and then you call CPUTIME, you may get a wraparound sooner than expected.
In MATLAB 6.5, the CPUTIME function simply returns the real time elapsed on Windows, and on UNIX it returns the system+user time in seconds as double. The accuracy on UNIX is system dependent and can be determined by finding out how many clock cycles per second the machine has, using the following C program:
/* compile this program by typing gcc thisfile.c
* run this program by typing ./a.out
*/
#include <unistd.h>
#include <sys/times.h>
main()
{
printf("clock cycles per second on this machine
%d\n",sysconf(_SC_CLK_TCK));
}
If it says 100, that means the resolution is 1/100 of a second.
You can look up POSIX functions like "sysconf" on the POSIX standard page:
Related Question