Skip to content

Instantly share code, notes, and snippets.

@abligh
Created March 24, 2016 20:50
Show Gist options
  • Save abligh/8c50f9700bfd47fa33f1 to your computer and use it in GitHub Desktop.
Save abligh/8c50f9700bfd47fa33f1 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <inttypes.h>
int
timeval_subtract(struct timeval *result, struct timeval *x,
struct timeval *y)
{
if (x->tv_usec < y->tv_usec) {
int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
y->tv_usec -= 1000000 * nsec;
y->tv_sec += nsec;
}
if (x->tv_usec - y->tv_usec > 1000000) {
int nsec = (x->tv_usec - y->tv_usec) / 1000000;
y->tv_usec += 1000000 * nsec;
y->tv_sec -= nsec;
}
result->tv_sec = x->tv_sec - y->tv_sec;
result->tv_usec = x->tv_usec - y->tv_usec;
return x->tv_sec < y->tv_sec;
}
int
main(int argc, char **argv)
{
struct timeval start;
struct timeval end;
struct timeval duration;
struct timeval timeout;
uint64_t uduration;
uint64_t i;
uint64_t maxduration = 0;
for (i = 0; i < 5000; i++) {
gettimeofday(&start, NULL);
timeout.tv_sec = 0;
timeout.tv_usec = 5000;
select(0, NULL, NULL, NULL, &timeout);
gettimeofday(&end, NULL);
timeval_subtract(&duration, &end, &start);
uduration = (uint64_t) duration.tv_usec + (uint64_t) duration.tv_sec * (uint64_t) 1000000;
if (uduration > maxduration)
maxduration = uduration;
}
printf("Maximum duration was %" PRIu64 " usecs\n", maxduration);
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment