OSDN Git Service

test: add a timer class
authorU. Artie Eoff <ullysses.a.eoff@intel.com>
Wed, 26 Oct 2016 20:24:15 +0000 (13:24 -0700)
committerXiang, Haihao <haihao.xiang@intel.com>
Mon, 31 Oct 2016 02:00:08 +0000 (10:00 +0800)
The timer is useful to quickly instrument various sections
of code during test development optimization tasks or other
timing needs.

Signed-off-by: U. Artie Eoff <ullysses.a.eoff@intel.com>
Reviewed-by: Sean V Kelley <seanvk@posteo.de>
(cherry picked from commit 1eae2167650c5159b510534d022cf79e38c9799c)

test/test_utils.h

index 0076677..333106c 100644 (file)
@@ -25,6 +25,7 @@
 #ifndef TEST_UTILS_H
 #define TEST_UTILS_H
 
+#include <chrono>
 #include <random>
 
 template <typename T>
@@ -46,4 +47,29 @@ private:
     std::uniform_int_distribution<T> dis;
 };
 
+class Timer
+{
+public:
+    typedef typename std::chrono::microseconds us;
+    typedef typename std::chrono::milliseconds ms;
+    typedef typename std::chrono::seconds       s;
+
+    Timer() { reset(); }
+
+    template <typename T = std::chrono::microseconds>
+    typename T::rep elapsed() const
+    {
+        return std::chrono::duration_cast<T>(
+            std::chrono::steady_clock::now() - start).count();
+    }
+
+    void reset()
+    {
+        start = std::chrono::steady_clock::now();
+    }
+
+private:
+    std::chrono::steady_clock::time_point start;
+};
+
 #endif // TEST_UTILS_H