Skip to content

Instantly share code, notes, and snippets.

@mathewmariani
Last active August 19, 2016 02:22
Show Gist options
  • Save mathewmariani/80d7e0de720b20e194585464cf5594e0 to your computer and use it in GitHub Desktop.
Save mathewmariani/80d7e0de720b20e194585464cf5594e0 to your computer and use it in GitHub Desktop.
A simple macro for quick benchmarking.
#pragma once
// https://gist.github.com/gongzhitaao/7062087
class Timer {
using clock_ = std::chrono::high_resolution_clock;
using milli_ = std::chrono::duration<double, std::milli>;
public:
Timer() : beg_(clock_::now()) {}
void reset() { beg_ = clock_::now(); }
double elapsed() const {
return std::chrono::duration_cast<milli_>
(clock_::now() - beg_).count();
}
private:
std::chrono::time_point<clock_> beg_;
};
#define MEASURE(__block, __itr) \
{ Timer _tmr; \
for (auto _i = 0; _i < __itr; ++_i) { __block } \
auto _t = _tmr.elapsed(); \
std::cout << _t << std::endl; }
// example
int main() {
MEASURE({
// some block of code
}, iterations);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment