aboutsummaryrefslogtreecommitdiffstats
path: root/time_diff.c
diff options
context:
space:
mode:
authorEliteTK <tomasz.kramkowski@gmail.com>2015-06-22 19:25:57 +0100
committerEliteTK <tomasz.kramkowski@gmail.com>2015-06-22 19:25:57 +0100
commitf942f2bcd4c8d0f90ff8dc18c89d9e8aa8b505ca (patch)
tree4a65e23e5052a6eece40768e9b332eb2f6c912c2 /time_diff.c
parentda87fcf25e0c94e57f00df84679cd6fadc56ed46 (diff)
parentc38dd32029b7fdc7cb9d1cc4427a43d9d5fb7374 (diff)
downloadc-stuff-f942f2bcd4c8d0f90ff8dc18c89d9e8aa8b505ca.tar.gz
c-stuff-f942f2bcd4c8d0f90ff8dc18c89d9e8aa8b505ca.tar.xz
c-stuff-f942f2bcd4c8d0f90ff8dc18c89d9e8aa8b505ca.zip
Merge branch 'master' of https://github.com/EliteTK/c-stuff
Diffstat (limited to 'time_diff.c')
-rw-r--r--time_diff.c60
1 files changed, 60 insertions, 0 deletions
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 <errno.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+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, &timespec) != 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;
+}