Skip to content

Instantly share code, notes, and snippets.

@ThePhD
Last active August 29, 2015 14:21
Show Gist options
  • Save ThePhD/9625a2faaca0137bc875 to your computer and use it in GitHub Desktop.
Save ThePhD/9625a2faaca0137bc875 to your computer and use it in GitHub Desktop.
Scopewatch and Stopwatch, they're so nice!
#pragma once
#include <chrono>
#include <functional>
namespace Furrovine {
template <typename Clock = std::chrono::high_resolution_clock, typename Fx = std::function<void(typename Clock::duration)>>
struct scopewatch {
public:
typedef Clock clock;
typedef typename clock::time_point time_point;
typedef typename clock::duration duration;
typedef typename clock::rep rep;
typedef typename clock::period period;
private:
Fx fx;
time_point start;
public:
template <typename... Tn>
scopewatch( Tn&&... argn ) : fx( std::forward<Tn>(argn...) ), start(clock::now()) {
}
~scopewatch() {
fx(clock::now() - start);
}
};
template <typename Clock = std::chrono::high_resolution_clock, typename Fx>
scopewatch<Clock, Fx> make_scopewatch( Fx&& reportfunction ) {
return scopewatch<Clock, Fx>( std::forward<Fx>( reportfunction ) );
}
}
#pragma once
#include <chrono>
namespace Furrovine {
template <typename Clock>
struct stopwatch {
public:
typedef Clock clock;
typedef typename clock::time_point time_point;
typedef typename clock::duration duration;
typedef typename clock::rep rep;
typedef typename clock::period period;
private:
time_point begin, end;
bool manualend;
public:
stopwatch() : begin(clock::now()), end(clock::now()), manualend(false) {
}
bool running() const {
return !manualend;
}
void start() {
begin = clock::now();
manualend = false;
}
duration elapsed() {
if (!manualend) {
end = clock::now();
}
return end - begin;
}
duration elapsed() const {
return end - begin;
}
void stop() {
end = clock::now();
manualend = true;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment