How do I measure program execution time in C? (Answer: the times function)

How do you measure the time taken by a program, or a program fragment? The general approach is simple (pseudocode):

time time_thing(f) {
  time t1 = now();
  do_thing();
  time t2 = now();
  return t2-t1;
}

The subtlety is in the semantics of your now function. As it turns out, C has many functions available to get the “current time”. The obvious one is gettimeofday:

#include <sys/time.h>
int gettimeofday(struct timeval *restrict tp, void *restrict tzp);  // The system's notion of the current Greenwich time and the current time zone

But gettimeofday is inappropriate for measuring the running time of your process. gettimeofday measures “wall-clock time”, which means you get a lot of noise due to other system activity, such as other processes and the kernel.

There is another function called times, which has another notion of “current time”: the amount of time “charged” to the current process since it began. The kernel “charges” processes by keeping a counter of how much CPU time they have used.

#include <sys/times.h>
struct tms {
  clock_t tms_utime;  // amount of CPU time charged to current process
  clock_t tms_stime;  // amount of CPU time charged to kernel on behalf of current process
  // ...
};
void times(struct tms * buf);  // Fill buf with current process charge

Notice the counters distinguish between time charged to the process, and time charged to the kernel on behalf of that process. The latter might include things like: setting up connections, setting up kernel queues.


I wrote this because I felt like it. This post is my own, and not associated with my employer.

Jim. Public speaking. Friends. Vidrio.