Timing programs with getrusage

Here is the template:
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/resource.h>

/** etc. **/

int main(int argc, char *argv[])
{
  struct rusage runtime;
  int t1,t2;

/** etc. **/

  getrusage(RUSAGE_SELF, &runtime);
  t1=(100*runtime.ru_utime.tv_sec)+(runtime.ru_utime.tv_usec/10000);

/** your program **/

  getrusage(RUSAGE_SELF, &runtime);
  t2=(100*runtime.ru_utime.tv_sec)+(runtime.ru_utime.tv_usec/10000);

  printf("\n");
  printf("Took %d.%d seconds\n", (t2-t1)/100, ((t2-t1)%100)/10);
  return 0;
}




Back to the table of contents of Vašek Chvátal's course notes