Switch to side-by-side view

--- a/sc2src/chrono.cpp
+++ b/sc2src/chrono.cpp
@@ -18,130 +18,82 @@
 
 // Measure and display time intervals.
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <time.h>
-#include <sys/time.h>
-#include <iostream>
+#include "chrono.h"
 
-#include "chrono.h"
+#include <chrono>
 
 using namespace std;
 
-////////////////////
-
-#ifndef CLOCK_REALTIME
-#define CLOCK_REALTIME 1
-#endif
-
-#define MILLIS(TS1, TS2)                                \
-    ((long long)((TS2).tv_sec - (TS1).tv_sec) * 1000LL +  \
-     ((TS2).tv_nsec - (TS1).tv_nsec) / 1000000)
-
-#define MICROS(TS1, TS2)                                        \
-    ((long long)((TS2).tv_sec - (TS1).tv_sec) * 1000000LL +       \
-     ((TS2).tv_nsec - (TS1).tv_nsec) / 1000)
-
-#define SECONDS(TS1, TS2)                             \
-    (float((TS2).tv_sec - (TS1).tv_sec) +             \
-     float((TS2).tv_nsec - (TS1).tv_nsec) * 1e-9)
-
-// We use gettimeofday instead of clock_gettime for now and get only
-// uS resolution, because clock_gettime is more configuration trouble
-// than it's worth
-static void gettime(int, Chrono::TimeSpec *ts)
-{
-    struct timeval tv;
-    gettimeofday(&tv, 0);
-    ts->tv_sec = tv.tv_sec;
-    ts->tv_nsec = tv.tv_usec * 1000;
-}
-///// End system interface (used to be much more complicated in the 199xs...)
-
-Chrono::TimeSpec Chrono::o_now;
+Chrono::TimePoint Chrono::o_now;
 
 void Chrono::refnow()
 {
-    gettime(CLOCK_REALTIME, &o_now);
-}
-
-long long Chrono::amicros() const
-{
-    TimeSpec ts;
-    ts.tv_sec = 0;
-    ts.tv_nsec = 0;
-    return MICROS(ts, m_orig);
+    o_now = chrono::steady_clock::now();
 }
 
 Chrono::Chrono()
+    : m_orig(chrono::steady_clock::now())
 {
-    restart();
 }
 
-// Reset and return value before rest in milliseconds
 long Chrono::restart()
 {
-    TimeSpec now;
-    gettime(CLOCK_REALTIME, &now);
-    long ret = MILLIS(m_orig, now);
-    m_orig = now;
-    return ret;
+    auto nnow = chrono::steady_clock::now();
+    auto ms =
+        chrono::duration_cast<chrono::milliseconds>(nnow - m_orig);
+    m_orig = nnow;
+    return ms.count();
 }
+
 long Chrono::urestart()
 {
-    TimeSpec now;
-    gettime(CLOCK_REALTIME, &now);
-    long ret = MICROS(m_orig, now);
-    m_orig = now;
-    return ret;
+    auto nnow = chrono::steady_clock::now();
+    auto ms =
+        chrono::duration_cast<chrono::microseconds>(nnow - m_orig);
+    m_orig = nnow;
+    return ms.count();
 }
 
-// Get current timer value, milliseconds
 long Chrono::millis(bool frozen)
 {
     if (frozen) {
-        return MILLIS(m_orig, o_now);
+        return chrono::duration_cast<chrono::milliseconds>
+            (o_now - m_orig).count();
     } else {
-        TimeSpec now;
-        gettime(CLOCK_REALTIME, &now);
-        return MILLIS(m_orig, now);
+        return chrono::duration_cast<chrono::milliseconds>
+            (chrono::steady_clock::now() - m_orig).count();
     }
 }
 
-//
 long Chrono::micros(bool frozen)
 {
     if (frozen) {
-        return MICROS(m_orig, o_now);
+        return chrono::duration_cast<chrono::microseconds>
+            (o_now - m_orig).count();
     } else {
-        TimeSpec now;
-        gettime(CLOCK_REALTIME, &now);
-        return MICROS(m_orig, now);
+        return chrono::duration_cast<chrono::microseconds>
+            (chrono::steady_clock::now() - m_orig).count();
     }
 }
 
 float Chrono::secs(bool frozen)
 {
     if (frozen) {
-        return SECONDS(m_orig, o_now);
+        return chrono::duration_cast<chrono::seconds>
+            (o_now - m_orig).count();
     } else {
-        TimeSpec now;
-        gettime(CLOCK_REALTIME, &now);
-        return SECONDS(m_orig, now);
+        return (chrono::duration_cast<chrono::seconds>
+                (chrono::steady_clock::now() - m_orig)).count();
     }
 }
 
 #else
 
-///////////////////// test driver
-
-
+// Test
 #include <stdio.h>
 #include <signal.h>
 #include <unistd.h>
 #include <stdlib.h>
-
-#include <iostream>
 
 #include "chrono.h"
 
@@ -161,7 +113,7 @@
 void
 showsecs(long msecs)
 {
-    fprintf(stderr, "%3.5f S", ((float)msecs) / 1000.0);
+    fprintf(stderr, "%3.5f S", (double(msecs)) / 1000.0);
 }
 
 void
@@ -174,9 +126,6 @@
     showsecs(achrono.millis());
     fprintf(stderr, ". Relative interval: ");
     showsecs(rchrono.restart());
-    cerr <<  " Abs micros: " << rchrono.amicros() <<
-        " Relabs micros: " << rchrono.amicros() - 1430477861905884LL
-         << endl;
     fprintf(stderr, ".\n");
     if (sig == SIGQUIT) {
         exit(0);
@@ -185,19 +134,13 @@
 
 int main(int argc, char **argv)
 {
-
     thisprog = argv[0];
-    argc--;
-    argv++;
-
+    argc--; argv++;
     if (argc != 0) {
         Usage();
     }
-
-    for (int i = 0; i < 50000000; i++);
-    fprintf(stderr, "Start secs: %.2f\n", achrono.secs());
-
-
+    sleep(1);
+    fprintf(stderr, "Initial micros: %ld\n", achrono.micros());;
     fprintf(stderr, "Type ^C for intermediate result, ^\\ to stop\n");
     signal(SIGINT, sigint);
     signal(SIGQUIT, sigint);