aboutsummaryrefslogtreecommitdiffstats
path: root/src/cmd/timer.cpp
diff options
context:
space:
mode:
authorlloyd <[email protected]>2014-11-12 01:23:55 +0000
committerlloyd <[email protected]>2014-11-12 01:23:55 +0000
commit8b0cbccc7b11e545ed27bc6d7bda04b5cf632e60 (patch)
tree7ea9368d6ccaa85337a63b55e8bd15efa46fd357 /src/cmd/timer.cpp
parent67161b91163afad417f9483cb557b26c5f5f4bc0 (diff)
Command line prog cleanup
Diffstat (limited to 'src/cmd/timer.cpp')
-rw-r--r--src/cmd/timer.cpp56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/cmd/timer.cpp b/src/cmd/timer.cpp
new file mode 100644
index 000000000..14eb47524
--- /dev/null
+++ b/src/cmd/timer.cpp
@@ -0,0 +1,56 @@
+/*
+* (C) 2009 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#include "timer.h"
+#include <chrono>
+#include <iomanip>
+
+void Timer::start()
+ {
+ stop();
+ m_timer_start = get_clock();
+ }
+
+void Timer::stop()
+ {
+ if(m_timer_start)
+ {
+ const u64bit now = get_clock();
+
+ if(now > m_timer_start)
+ m_time_used += (now - m_timer_start);
+
+ m_timer_start = 0;
+ ++m_event_count;
+ }
+ }
+
+u64bit Timer::get_clock()
+ {
+ auto now = std::chrono::high_resolution_clock::now().time_since_epoch();
+ return std::chrono::duration_cast<std::chrono::nanoseconds>(now).count();
+ }
+
+std::ostream& operator<<(std::ostream& out, Timer& timer)
+ {
+ //out << timer.value() << " ";
+
+ double events_per_second_fl =
+ static_cast<double>(timer.events() / timer.seconds());
+
+ u64bit events_per_second = static_cast<u64bit>(events_per_second_fl);
+
+ out << events_per_second << " " << timer.get_name() << " per second; ";
+
+ std::string op_or_ops = (timer.events() == 1) ? "op" : "ops";
+
+ out << std::setprecision(2) << std::fixed
+ << timer.ms_per_event() << " ms/op"
+ << " (" << timer.events() << " " << op_or_ops << " in "
+ << timer.milliseconds() << " ms)";
+
+ return out;
+ }