Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
gettimeofday() benchmark.
#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;
}
@homm
Copy link

homm commented Mar 28, 2018

Thanks!

$ ./a.out 
gettimeofday() = 21 ns

$ cat /etc/issue
Ubuntu 14.04.5 LTS

$ cat /proc/cpuinfo | grep model
model name	: Intel(R) Core(TM) i5-4258U CPU @ 2.40GHz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment