Reimplement clock(3) using clock_gettime(3)
Unlike times(), clock_gettime() is implemented as a vDSO on many architectures.
So, using clock_gettime() will return a more accurate time and do so with less
overhead because it does have the overhead of calling into the kernel.
It is also significantly more accurate because it measures the actual time in
nanoseconds rather than the number of ticks (typically 1 millisecond or more).
Bug: 17814435
Change-Id: Id4945d9f387330518f78669809639952e9227ed9
diff --git a/tests/time_test.cpp b/tests/time_test.cpp
index c631b6a..d2fec9a 100644
--- a/tests/time_test.cpp
+++ b/tests/time_test.cpp
@@ -404,3 +404,11 @@
ASSERT_EQ(0, ts2.tv_sec);
ASSERT_LT(ts2.tv_nsec, 1000000);
}
+
+TEST(time, clock) {
+ // clock(3) is hard to test, but a 1s sleep should cost less than 1ms.
+ clock_t t0 = clock();
+ sleep(1);
+ clock_t t1 = clock();
+ ASSERT_LT(t1 - t0, CLOCKS_PER_SEC / 1000);
+}