Commit 778b1529 authored by Zhiping Jiang (蒋志平)'s avatar Zhiping Jiang (蒋志平) 💬

Replace Singleton and TickTockTimer with OpenCVExtensions.

Update TickTockTimer.
parent 1a362ef6
......@@ -9,23 +9,56 @@
#include "TickTockTimer.h"
namespace cve {
void TickTockTimer::tick() {
startTime = system_clock::now();
std::map<std::string, TickTockTimer *> TickTockTimer::instanceMap;
TickTockTimer & TickTockTimer::tick() {
startTime = steady_clock::now();
return *this;
}
void TickTockTimer::tock() {
time_point<system_clock> endTime = system_clock::now();
TickTockTimer & TickTockTimer::tock() {
time_point<steady_clock> endTime = steady_clock::now();
duration<double> elipsed_seconds = endTime - startTime;
durations.push_back(elipsed_seconds.count());
auto duration_double = elipsed_seconds.count();
durations.push_back(duration_double);
if (durations.size() > durationLength) {
durations.pop_front();
}
auto averageDuration = [&] () {
return std::accumulate(durations.begin(), durations.end(), 0.0) / durations.size();
}();
std::printf("Timer(%s) %.4fs %.1ffps\n", timerName.c_str(), averageDuration, 1.0/averageDuration);
return *this;
}
TickTockTimer & TickTockTimer::getInstance(const std::string &timerName, int timerLength) {
if (instanceMap.find(timerName) != instanceMap.end()) {
return *instanceMap[timerName];
} else {
auto newTimer = new TickTockTimer(timerName, timerLength);
instanceMap[timerName] = newTimer;
return *instanceMap[timerName];
}
}
TickTockTimer &TickTockTimer::tock_instant() {
time_point<steady_clock> endTime = steady_clock::now();
duration<double> elipsed_seconds = endTime - startTime;
auto duration_double = elipsed_seconds.count();
std::printf("Instant Timer(%s) %.9fs \n", timerName.c_str(), duration_double);
return *this;
}
TickTockTimer &TickTockTimer::tickTockLambda(std::function<void(void)> codeBlock) {
this->tick();
codeBlock();
this->tock_instant();
return *this;
}
}
......@@ -11,23 +11,29 @@
#include <iostream>
#include <chrono>
#include <thread>
#include <numeric>
#include <deque>
#include <map>
#include <string>
using namespace std::chrono;
namespace cve {
class TickTockTimer {
private:
static std::map<std::string, TickTockTimer *> instanceMap;
std::string timerName;
std::deque<double> durations;
int durationLength = 50;
time_point<system_clock> startTime;
time_point<steady_clock> startTime;
public:
TickTockTimer(const std::string & timerName, int timerLength = 50): timerName{timerName}, durationLength{timerLength} {}
void tick();
void tock();
static TickTockTimer & getInstance(const std::string & timerName, int timerLength = 50);
TickTockTimer & tick();
TickTockTimer & tock();
TickTockTimer & tock_instant();
TickTockTimer & tickTockLambda(std::function<void(void)> codeBlock);
};
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment