[ccan] [PATCH 1/2] time: Convenience functions for measuring elapsed time

David Gibson david at gibson.dropbear.id.au
Mon Jun 2 22:03:02 EST 2014


Add simple time_since() and time_since_msec() to easily retrieve the
elapsed time from a timestamp previously recorded with time_now().

Signed-off-by: David Gibson <david at gibson.dropbear.id.au>
---
 ccan/time/test/run-check.c |  9 ++++++++-
 ccan/time/test/run.c       |  9 ++++++++-
 ccan/time/time.h           | 32 ++++++++++++++++++++++++++++++++
 3 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/ccan/time/test/run-check.c b/ccan/time/test/run-check.c
index 37763ab..d30e10b 100644
--- a/ccan/time/test/run-check.c
+++ b/ccan/time/test/run-check.c
@@ -19,7 +19,7 @@ int main(void)
 	struct timespec t1, t2, t3, zero = { 0, 0 };
 	int fds[2];
 
-	plan_tests(62);
+	plan_tests(64);
 
 	/* Test time_now */
 	t1 = time_now();
@@ -151,6 +151,13 @@ int main(void)
 	ok1(t2.tv_sec == 2);
 	ok1(t2.tv_nsec == 147483648);
 
+	/* Interval convenience functions */
+	t1 = time_now();
+	t3.tv_sec = 1;
+	t3.tv_nsec = 0;
+	ok1(time_less(time_since(t1), t3));
+	ok1(time_since_msec(t1) < 1000);
+
 	pipe(fds);
 
 	fflush(stdout);
diff --git a/ccan/time/test/run.c b/ccan/time/test/run.c
index 21c7221..c0ac130 100644
--- a/ccan/time/test/run.c
+++ b/ccan/time/test/run.c
@@ -6,7 +6,7 @@ int main(void)
 {
 	struct timespec t1, t2, t3, zero = { 0, 0 };
 
-	plan_tests(61);
+	plan_tests(63);
 
 	/* Test time_now */
 	t1 = time_now();
@@ -144,5 +144,12 @@ int main(void)
 	ok1(t2.tv_sec == 2);
 	ok1(t2.tv_nsec == 147483648);
 
+	/* Interval convenience functions */
+	t1 = time_now();
+	t3.tv_sec = 1;
+	t3.tv_nsec = 0;
+	ok1(time_less(time_since(t1), t3));
+	ok1(time_since_msec(t1) < 1000);
+
 	return exit_status();
 }
diff --git a/ccan/time/time.h b/ccan/time/time.h
index e4298d0..dc1a0c3 100644
--- a/ccan/time/time.h
+++ b/ccan/time/time.h
@@ -211,6 +211,23 @@ struct timespec time_divide(struct timespec t, unsigned long div);
 struct timespec time_multiply(struct timespec t, unsigned long mult);
 
 /**
+ * time_since - elapsed time since another tipespec
+ * @start: a timespec
+ *
+ * Example:
+ *	struct timespec start = time_now();
+ *	struct timespec ts;
+ *
+ *	...
+ *	ts = time_since(start);
+ *	printf("stuff took %lu.%09lus\n", (long)ts.tv_sec, (long)ts.tv_nsec);
+ */
+static inline struct timespec time_since(struct timespec start)
+{
+	return time_sub(time_now(), start);
+}
+
+/**
  * time_to_sec - return number of seconds
  * @t: a time
  *
@@ -397,4 +414,19 @@ static inline struct timespec timeval_to_timespec(struct timeval tv)
 	ts.tv_nsec = tv.tv_usec * 1000;
 	return ts;
 }
+
+/**
+ * time_since_msec - number of milliseconds elapsed since an earlier time
+ * @start: a time
+ *
+ * Example:
+ *	struct timespec start = time_now();
+ *	...
+ *	printf("Took %u msec\n", (unsigned)time_since_msec(start));
+ */
+static inline uint64_t time_since_msec(struct timespec start)
+{
+	return time_to_msec(time_since(start));
+}
+
 #endif /* CCAN_TIME_H */
-- 
1.9.3



More information about the ccan mailing list