Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save damiandragowski/ad3de0f98e341d491b72de79895df9f8 to your computer and use it in GitHub Desktop.
Save damiandragowski/ad3de0f98e341d491b72de79895df9f8 to your computer and use it in GitHub Desktop.
gettimeofday
#include <stdio.h>
#include <sys/time.h>
#define NUMBER 30
int main (void) {
struct timeval tv[NUMBER];
int count[NUMBER], i, diff;
gettimeofday (&tv[0], NULL);
for (i = 1; i < NUMBER; i++) {
gettimeofday (&tv[i], NULL);
count[i] = 1;
while ((tv[i].tv_sec == tv[i-1].tv_sec) &&
(tv[i].tv_usec == tv[i-1].tv_usec))
{
gettimeofday (&tv[i], NULL);
count[i]++;
}
}
printf ("%2d: secs = %d, usecs = %6d\n", 0, tv[0].tv_sec, tv[0].tv_usec);
for (i = 1; i < NUMBER; i++) {
diff = (tv[i].tv_sec - tv[i-1].tv_sec) * 1000000;
diff += tv[i].tv_usec - tv[i-1].tv_usec;
printf ("%2d: alg=%ld secs = %d, usecs = %6d, count = %5d, diff = %d\n",
i, (int) (tv->tv_sec * 1000 + tv->tv_usec / 1000), tv[i].tv_sec, tv[i].tv_usec, count[i], diff);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment