Question

In the Matlab documentation for cputime, it says:

Although it is possible to measure performance using the cputime function, it is recommended that you use the tic and toc functions for this purpose exclusively. It has been the general rule for CPU-intensive calculations run on Microsoft® Windows® machines that the elapsed time using cputime and the elapsed time using tic and toc are close in value, ignoring any first time costs. There are cases however that show a significant difference between these two methods. For example, in the case of a Pentium 4 with hyperthreading running Windows, there can be a significant difference between the values returned by cputime versus tic and toc.

I have an Intel Core i5 running linux. I want to measure the CPU time of a Matlab script. tic and toc return real time, which is affected by other processes running in the system. Should I use cputime?

Note: In the documentation it also says that cputime overflows and wraps around whenever it hits the internal representation limit. This doesn't affect me because I know that my program execution time is shorter than the internal representation limit.

Was it helpful?

Solution

Tic and Toc are the functions being usually used because they provide better resolution, which is important for timing small calculations.

In order to get around the problem of variance in system resource usage by other processes, you can run the computation being profiled a large enough number of times so that averaging the results will smooth out the noise.

if the calculation is very small, you can run it in a loop between each pair of tic and toc calls. The size of this inner loop can be determined experimentally by specifying a desired runtime of the tic-toc pair and increasing the inner loop size until it is achieved.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top