aboutsummaryrefslogtreecommitdiff
path: root/examples/performance/time.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2014-04-30 19:52:18 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2014-04-30 19:52:18 +0200
commit960adfbc3e19b684f5f3839782f2c1399eabd533 (patch)
tree84abb6d637abf7fbafc58f1fa5f73340c76669df /examples/performance/time.cxx
parent387fbf589e73d96af9050afa121ef8b5e5370c4e (diff)
Add performance example
Diffstat (limited to 'examples/performance/time.cxx')
-rw-r--r--examples/performance/time.cxx46
1 files changed, 46 insertions, 0 deletions
diff --git a/examples/performance/time.cxx b/examples/performance/time.cxx
new file mode 100644
index 0000000..cfb6d09
--- /dev/null
+++ b/examples/performance/time.cxx
@@ -0,0 +1,46 @@
+// file : examples/performance/time.cxx
+// copyright : not copyrighted - public domain
+
+#include "time.hxx"
+
+#if defined (WIN32) || defined (__WIN32__)
+# define WIN32_LEAN_AND_MEAN
+# include <windows.h> // GetSystemTimeAsFileTime
+#else
+# include <time.h> // gettimeofday
+# include <sys/time.h> // timeval
+#endif
+
+#include <ostream> // std::ostream
+#include <iomanip> // std::setfill, std::setw
+
+namespace os
+{
+ time::
+ time ()
+ {
+#if defined (WIN32) || defined (__WIN32__)
+ FILETIME ft;
+ GetSystemTimeAsFileTime (&ft);
+ unsigned long long v (
+ ((unsigned long long) (ft.dwHighDateTime) << 32) + ft.dwLowDateTime);
+
+ sec_ = static_cast<unsigned long> (v / 10000000ULL);
+ nsec_ = static_cast<unsigned long> ((v % 10000000ULL) * 100);
+#else
+ timeval tv;
+ if (gettimeofday(&tv, 0) != 0)
+ throw failed ();
+
+ sec_ = static_cast<unsigned long> (tv.tv_sec);
+ nsec_ = static_cast<unsigned long> (tv.tv_usec * 1000);
+#endif
+ }
+
+ std::ostream&
+ operator<< (std::ostream& o, time const& t)
+ {
+ return o << t.sec () << '.'
+ << std::setfill ('0') << std::setw (9) << t.nsec ();
+ }
+}