aboutsummaryrefslogtreecommitdiff
path: root/external/glim/NsecTimer.hpp
diff options
context:
space:
mode:
authorThomas Winget <tewinget@gmail.com>2014-10-13 18:52:45 -0400
committerwarptangent <warptangent@inbox.com>2015-01-04 18:41:44 -0800
commit90d6f8bf62bca97dc911b30505252becd8ef7520 (patch)
treeb87d425251db5171dd66f7a83e8f009ead72b176 /external/glim/NsecTimer.hpp
parentupdate new blockchain to build with new changes (diff)
downloadmonero-90d6f8bf62bca97dc911b30505252becd8ef7520.tar.xz
Adding libglim as an external library
libglim is an Apache-licensed C++ wrapper for lmdb, and rather than rolling our own it seems prudent to use it. Note: lmdb is not included in it, and unless something happens as did with libunbound, should be installed via each OS' package manager or equivalent.
Diffstat (limited to 'external/glim/NsecTimer.hpp')
-rw-r--r--external/glim/NsecTimer.hpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/external/glim/NsecTimer.hpp b/external/glim/NsecTimer.hpp
new file mode 100644
index 000000000..e66126410
--- /dev/null
+++ b/external/glim/NsecTimer.hpp
@@ -0,0 +1,51 @@
+#ifndef _NSEC_TIMER_H
+#define _NSEC_TIMER_H
+
+#include <time.h> // clock_gettime, CLOCK_MONOTONIC
+#include <stdint.h>
+#include <string>
+#include <sstream>
+
+namespace glim {
+
+//! Safe nanoseconds timer.
+struct NsecTimer {
+ timespec start;
+ NsecTimer () {restart();}
+ //! Nanoseconds since the creation or restart of the timer.
+ int64_t operator()() const {
+ timespec nsecStop; clock_gettime (CLOCK_MONOTONIC, &nsecStop);
+ return (int64_t) (nsecStop.tv_sec - start.tv_sec) * 1000000000LL + (int64_t) (nsecStop.tv_nsec - start.tv_nsec);
+ }
+ /** Seconds since the creation or restart of the timer. */
+ double sec() const {
+ timespec nsecStop; clock_gettime (CLOCK_MONOTONIC, &nsecStop);
+ double seconds = nsecStop.tv_sec - start.tv_sec;
+ seconds += (double)(nsecStop.tv_nsec - start.tv_nsec) / 1000000000.0;
+ return seconds;
+ }
+ //! Seconds since the creation or restart of the timer.
+ std::string seconds (int precision = 9) const {
+ // The trick is to avoid the scientific notation by printing integers.
+ double sec = this->sec();
+ std::ostringstream buf;
+ int isec = (int) sec;
+ buf << isec;
+
+ sec -= isec;
+ for (int pc = precision; pc; --pc) sec *= 10.0;
+ int ifrac = (int) sec;
+ if (ifrac > 0) {
+ buf << '.';
+ buf.fill ('0'); buf.width (precision);
+ buf << ifrac;
+ }
+ return buf.str();
+ }
+ void restart() {clock_gettime (CLOCK_MONOTONIC, &start);}
+ int64_t getAndRestart() {int64_t tmp = operator()(); restart(); return tmp;}
+};
+
+}
+
+#endif // _NSEC_TIMER_H