Last active
October 21, 2015 15:25
-
-
Save Dekker1/6b17ad93dcf07df3020d to your computer and use it in GitHub Desktop.
A simple timer for C++ based programs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by Jip J. Dekker on 05/10/15. | |
// | |
#include "Timer.h" | |
void Timer::start() { | |
if (output) cout << "\t Start Application"<< endl; | |
app.start = high_resolution_clock::now(); | |
} | |
void Timer::start(string event) { | |
if (output) cout << "\t\t Start: " << event << endl; | |
TimeEvent* t = &timers[event]; | |
t->start = high_resolution_clock::now(); | |
} | |
void Timer::stop() { | |
app.stop = high_resolution_clock::now(); | |
app.time_span[0] = duration_cast<duration<double, milli>>(app.stop - app.start); | |
app.runs = 1; | |
if (output) cout << "\t Stop Application"<< endl; | |
} | |
void Timer::stop(string event, int run) { | |
TimeEvent* t = &timers[event]; | |
t->stop = high_resolution_clock::now(); | |
t->time_span[run] = duration_cast<duration<double, milli>>(t->stop - t->start); | |
t->runs = t->runs < run ? run : t->runs; | |
if (output) cout << "\t\t Stop: " << event << endl; | |
} | |
void Timer::dump(ostream& s, bool group_runs) { | |
s.width(10); | |
s << "-++== " << title << " ==++-" << endl; | |
if (group_runs) { | |
dump_runs(s); | |
} else { | |
dump_events(s); | |
} | |
} | |
void Timer::dump_runs(ostream& s) { | |
int max_runs = 0; // We assume at least one run. | |
for (int i = 0; i <= max_runs; ++i) { | |
s << endl; | |
s.width(20); | |
s << "=== Run " << i << ": ===" << endl; | |
for (auto &t : timers) { | |
s.width(30); | |
max_runs = max_runs < t.second.runs ? t.second.runs : max_runs; | |
if(t.second.time_span.find(i) != t.second.time_span.end()) { | |
s << left << t.first + ":"; | |
s << right << t.second.time_span[i].count() << " ms" << endl; | |
} | |
} | |
} | |
s << endl; | |
s.width(30); | |
s << "=== Total Times: ===" << endl; | |
for (auto &t : timers) { | |
duration<double, milli> total = high_resolution_clock::duration::zero(); | |
for (auto &time : t.second.time_span) { | |
total += time.second; | |
} | |
s.width(30); | |
s << left << t.first + ":"; | |
s << total.count() << " ms" << endl; | |
} | |
s << endl; | |
s.width(30); | |
s << internal << "=== Average Times: ===" << endl; | |
for (auto &t : timers) { | |
duration<double, milli> total = high_resolution_clock::duration::zero(); | |
for (auto &time : t.second.time_span) { | |
total += time.second; | |
} | |
s.width(30); | |
s << left << t.first + ":"; | |
s << right << total.count()/t.second.time_span.size() << " ms" << endl; | |
} | |
s << endl << endl; | |
s.width(30); | |
s << left<< "Total runtime:"; | |
s << right << app.time_span[0].count() << " ms" << endl; | |
} | |
void Timer::dump_events(ostream& s) { | |
for (auto &t : timers) { | |
s << endl; | |
s.width(10); | |
s << "=== " << t.first << ": ===" << endl; | |
duration<double, milli> total = high_resolution_clock::duration::zero(); | |
for (auto &time : t.second.time_span) { | |
s.width(20); | |
s << left << "Run " + to_string(time.first) + ":"; | |
s << right << time.second.count() << " ms" << endl; | |
total += time.second; | |
} | |
s.width(20); | |
s << left<< "Total:"; | |
s << right << total.count() << " ms" << endl; | |
s.width(20); | |
s << left << "Average:"; | |
s << right << total.count()/t.second.time_span.size() << " ms" << endl; | |
} | |
s << endl << endl; | |
s.width(20); | |
s << left<< "Total runtime:"; | |
s << right << app.time_span[0].count() << " ms" << endl; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by Jip J. Dekker on 05/10/15. | |
// | |
#ifndef PROJECT_TIMER_H | |
#define PROJECT_TIMER_H | |
#include <chrono> | |
#include <string> | |
#include <map> | |
#include <vector> | |
#include <iostream> | |
using namespace std; | |
using namespace std::chrono; | |
class Timer { | |
public: | |
Timer(string title, bool output = false){this->output = output; this->title = title;}; | |
virtual ~Timer(){}; | |
// Start Application | |
virtual void start(); | |
// Start the timer for (new) event with name |event| | |
virtual void start(string event); | |
// Stop Application | |
virtual void stop(); | |
// Stop the timer for event with name |event| and save time with run number | |
virtual void stop(string event, int run); | |
// Output all timed events to the ostream. | |
virtual void dump(ostream& s, bool group_runs = false); | |
protected: | |
class TimeEvent { | |
public: | |
TimeEvent(){runs = 0;} | |
high_resolution_clock::time_point start, stop; | |
map<int, duration<double, milli>> time_span; | |
int runs; | |
}; | |
map<string, TimeEvent> timers; | |
TimeEvent app; | |
bool output; | |
string title; | |
private: | |
void dump_runs(ostream& s); | |
void dump_events(ostream& s); | |
}; | |
#endif //PROJECT_TIMER_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment