Skip to content

Instantly share code, notes, and snippets.

@Yawning
Created January 2, 2015 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yawning/2829f9adf889f1334005 to your computer and use it in GitHub Desktop.
Save Yawning/2829f9adf889f1334005 to your computer and use it in GitHub Desktop.
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