Last active
August 19, 2016 02:22
-
-
Save mathewmariani/80d7e0de720b20e194585464cf5594e0 to your computer and use it in GitHub Desktop.
A simple macro for quick benchmarking.
This file contains 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
#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