From a72bf3a44e8002181b2818ab557434a2a0a22c96 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Fri, 19 Jun 2015 19:15:27 +0100 Subject: nkey.c; time_diff.c --- time_diff.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 time_diff.c (limited to 'time_diff.c') diff --git a/time_diff.c b/time_diff.c new file mode 100644 index 0000000..726a6cd --- /dev/null +++ b/time_diff.c @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include +#include +#include + +const char *progname; + +inline uint64_t get_time_ms(void) +{ + struct timespec timespec; + static const uint64_t invmil = 1 / 1000000; + + if (clock_gettime(CLOCK_MONOTONIC, ×pec) != 0) { + fprintf(stderr, "%s: Unable to get time delta: %s\n", + progname, strerror(errno)); + exit(EXIT_FAILURE); + } + + return timespec.tv_sec * 1000 + timespec.tv_nsec * invmil; +} + +uint64_t get_delta(void) +{ + static uint64_t oldtime = 0; + uint64_t timediff, newtime; + + newtime = get_time_ms(); + + timediff = newtime - oldtime; + + oldtime = newtime; + + return timediff; +} + +int main(int argc, char **argv) +{ + struct { + int a; + char b; + } teststruct = {1, 2}; + + uint64_t buildup = 0, count = 0; + + progname = argc != 0 && argv[0] != NULL ? argv[0] : ""; + + get_delta(); + + while (true) { + for (buildup += get_delta(); buildup >= 1000; buildup -= 1000) + printf("%lu, %lu\n", count++, buildup); + } + + printf("%d\n", teststruct.a); + + return 0; +} -- cgit v1.2.3-54-g00ecf