Created
January 2, 2015 10:41
-
-
Save Yawning/2829f9adf889f1334005 to your computer and use it in GitHub Desktop.
gettimeofday() benchmark.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
#include <stdio.h> | |
#include <time.h> | |
#include <sys/time.h> | |
#include <assert.h> | |
#define ITERS 1000000000 | |
static uint64_t gettime(void) { | |
struct timespec ts; | |
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { | |
assert(0); | |
} | |
return ((uint64_t)ts.tv_sec) * 1000 * 1000 * 1000 + ts.tv_nsec; | |
} | |
int main(int argc, char *argv[]) { | |
struct timeval tv; | |
uint64_t start_time; | |
uint64_t end_time; | |
int i; | |
(void)argc; | |
(void)argv; | |
start_time = gettime(); | |
for (i = 0; i < ITERS; ++i) { | |
gettimeofday(&tv, NULL); | |
} | |
end_time = gettime(); | |
fprintf(stdout, "gettimeofday() = %llu ns\n", (end_time - start_time) / ITERS); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!