summaryrefslogtreecommitdiff
path: root/src/lib/timer.cc
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2025-01-21 10:25:46 +0100
committerCarl Hetherington <cth@carlh.net>2025-01-21 10:25:46 +0100
commit81915d4db00c333da2debd2f8e9a0c02f7a316a3 (patch)
tree68062af342d2ef6c7a3b5aebbe7cb761bbab5372 /src/lib/timer.cc
parent4c905330c2052cd77be09d9deb301f1fcf4b81f2 (diff)
Diffstat (limited to 'src/lib/timer.cc')
-rw-r--r--src/lib/timer.cc50
1 files changed, 49 insertions, 1 deletions
diff --git a/src/lib/timer.cc b/src/lib/timer.cc
index caef89e0e..46766d70f 100644
--- a/src/lib/timer.cc
+++ b/src/lib/timer.cc
@@ -27,6 +27,7 @@
#include "compose.hpp"
#include "timer.h"
#include "util.h"
+#include <fmt/format.h>
#include <sys/time.h>
#include <iostream>
@@ -41,6 +42,9 @@ using std::string;
using boost::optional;
+GlobalTimer* GlobalTimer::_instance = nullptr;
+
+
/** @param n Name to use when giving output */
PeriodTimer::PeriodTimer (string n)
: _name (n)
@@ -54,7 +58,7 @@ PeriodTimer::~PeriodTimer ()
{
struct timeval stop;
gettimeofday (&stop, 0);
- cout << N_("T: ") << _name << N_(": ") << (seconds (stop) - seconds (_start)) << N_("\n");
+ cout << fmt::format("T: {}: {:.3f}\n", _name, seconds(stop) - seconds(_start));
}
@@ -143,3 +147,47 @@ StateTimer::~StateTimer ()
cout << N_("\t") << i.second << "\n";
}
}
+
+
+GlobalTimer*
+GlobalTimer::instance()
+{
+ if (!_instance) {
+ _instance = new GlobalTimer();
+ }
+
+ return _instance;
+}
+
+
+void
+GlobalTimer::reset()
+{
+ struct timeval now;
+ gettimeofday(&now, 0);
+ _reset = _last = seconds(now);
+ // std::cout << "|----------------------\\\n";
+}
+
+
+void
+GlobalTimer::mark(string const& tag)
+{
+ // std::cout << "|" << tag << " ";
+
+ struct timeval now;
+ gettimeofday(&now, 0);
+ auto const now_seconds = seconds(now);
+
+ if (_last > 0) {
+ // std::cout << fmt::format("{:.2} ", now_seconds - _last);
+ }
+
+ if (_reset > 0) {
+ // std::cout << fmt::format("{:.2} ", now_seconds - _reset);
+ }
+
+ _last = now_seconds;
+ // std::cout << "\n";
+}
+