/* Compile with: gcc -Wall -o times times.c -lrt */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/times.h>

int main(void)
  {
  double tbase;
  struct tms tms0, tms1;
  int i, j;
  float s;
  FILE *f;

  /* Anfangswerte der Zeiten (in der Regel 0) */
  if (times (&tms0) == -1)
    perror ("times");

  /* Zeitbasis in Sekunden ermitteln */
  tbase = (double)(1.0/sysconf(_SC_CLK_TCK));
  printf("Zeitbasis: _SC_CLK_TCK: %ld, Tbase: %lf Sekunden\n",
        sysconf(_SC_CLK_TCK),tbase);

  /* sinnlose Operationen */
  for (i = 0; i < 1000; i++)
    {
    for (j = 0; j < 100000; j++)
      s = (double)(i*j);
    f = fopen("/bin/ls","r");
    fclose(f);
    }

  /* Endwerte der Zeiten */
  if (times (&tms1) == -1)
    perror ("times");

  /* Ausgabe in clock ticks */
  printf("times 0 - usr: %ld sys: %ld ch-usr: %ld ch-sys: %ld\n",
        tms0.tms_utime, tms0.tms_stime, tms0.tms_cutime, tms0.tms_cstime);
  printf("times 1 - usr: %ld sys: %ld ch-usr: %ld ch-sys: %ld\n",
        tms1.tms_utime, tms1.tms_stime, tms1.tms_cutime, tms1.tms_cstime);

  /* Ausgabe in Sekunden */
  printf("user: %lf system: %lf\n",
        tbase*(tms1.tms_utime - tms0.tms_utime),
        tbase*(tms1.tms_stime - tms0.tms_stime));

  return 0;
  }
              