]> Cypherpunks repositories - keks.git/commitdiff
Include run time in totals
authorSergey Matveev <stargrave@stargrave.org>
Mon, 16 Dec 2024 15:23:37 +0000 (18:23 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Mon, 16 Dec 2024 15:23:37 +0000 (18:23 +0300)
c/cmd/print-items/print-items.c

index d3ad628e58aeef0432e842cbd18bad292082ff90041a58590a6a98d4f059f681..4df28424c65bfd3635b774e7e8d167f83b5f60ecff050b0e4f391586964704ca 100644 (file)
@@ -21,6 +21,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <time.h>
 #include <unistd.h>
 
 #include <keks/dec.h>
@@ -271,6 +272,7 @@ main(int argc, char **argv)
 {
     bool doEncode = false;
     bool noTotals = false;
+    bool onlyTotals = false;
     ptrdiff_t itemsInitialLen = 2048;
     NoColour = getenv("NO_COLOR") != NULL;
     struct option longopts[] = {
@@ -278,6 +280,7 @@ main(int argc, char **argv)
         {"do-encode", no_argument, NULL, 'b'},
         {"no-offsets", no_argument, NULL, 'c'},
         {"no-totals", no_argument, NULL, 'd'},
+        {"only-totals", no_argument, NULL, 'e'},
         {"items-initial-len", required_argument, NULL, 'f'},
         {NULL, 0, NULL, 0}};
     int ch = 0;
@@ -310,6 +313,9 @@ main(int argc, char **argv)
         case 'd': // no-totals
             noTotals = true;
             break;
+        case 'e': // only-totals
+            onlyTotals = true;
+            break;
         case 'f': { // items-initial-len
             errno = 0;
             long tmp = strtol(optarg, NULL, 10);
@@ -354,21 +360,37 @@ main(int argc, char **argv)
         items.offsets = NULL;
     }
     size_t off = 0;
+    struct timespec started;
+    struct timespec finished;
+    errno = 0;
+    if (clock_gettime(CLOCK_MONOTONIC_PRECISE, &started) != 0) {
+        fprintf(stderr, "clock_gettime(started): %s\n", strerror(errno));
+        exit(EXIT_FAILURE);
+    }
     err = KEKSItemsParse(&items, &off, buf, len);
-    if (err != KEKSErrNo) {
-        fprintf(stderr, "err: %s\n", KEKSErr2Str(err));
-        return EXIT_FAILURE;
+    errno = 0;
+    if (clock_gettime(CLOCK_MONOTONIC_PRECISE, &finished) != 0) {
+        fprintf(stderr, "clock_gettime(finished): %s\n", strerror(errno));
+        exit(EXIT_FAILURE);
     }
-    err = printer(&items, 0, 0, false, 0, NULL);
     if (err != KEKSErrNo) {
         fprintf(stderr, "err: %s\n", KEKSErr2Str(err));
         return EXIT_FAILURE;
     }
+    if (!onlyTotals) {
+        err = printer(&items, 0, 0, false, 0, NULL);
+        if (err != KEKSErrNo) {
+            fprintf(stderr, "err: %s\n", KEKSErr2Str(err));
+            return EXIT_FAILURE;
+        }
+    }
     if (!noTotals) {
         printf(
-            "items: %zu  size: %zu\n",
+            "items: %zu  size: %zu  time: %zuns\n",
             items.len,
-            items.len * (sizeof(struct KEKSItem) + (NoOffsets ? 0 : sizeof(size_t))));
+            items.len * (sizeof(struct KEKSItem) + (NoOffsets ? 0 : sizeof(size_t))),
+            1000000000 * (finished.tv_sec - started.tv_sec) + finished.tv_nsec -
+                started.tv_nsec);
     }
     if (off < len) {
         char *hex = HexEnc(buf + off, len - off);