Skip to content

Instantly share code, notes, and snippets.

@jtilly
Created November 29, 2016 16:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtilly/a423be999929d70406489a4103e67453 to your computer and use it in GitHub Desktop.
Save jtilly/a423be999929d70406489a4103e67453 to your computer and use it in GitHub Desktop.
Simple C++ Profiler Class
// timer.h
#include <iostream>
#include <sstream>
#ifndef timer_h
#define timer_h
class timer {
private:
std::chrono::steady_clock::time_point begin;
bool verbose;
std::stringstream logfile;
public:
timer();
timer(bool b);
void start();
void toc();
void toc(std::string s);
void stop();
void stop(std::string s);
void dump();
};
timer::timer() {
verbose = true;
this->start();
}
timer::timer(bool b) {
verbose = b;
this->start();
}
void timer::start() {
begin = std::chrono::steady_clock::now();
}
void timer::stop(std::string s) {
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
if(verbose) {
Rcout << s.c_str() << " Time elapsed = " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count()/1e6 << std::endl;
}
logfile << s << "," << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() / 1e6 << std::endl;
}
void timer::stop() {
this->stop("");
}
void timer::toc() {
this->toc("");
}
void timer::toc(std::string s) {
this->stop(s);
this->start();
}
void timer::dump() {
ofstream myfile;
myfile.open("/tmp/timer.csv");
myfile << logfile.rdbuf();
myfile.close();
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment