Created
October 4, 2019 08:18
-
-
Save andreysolovyev381/76fbf9b41cd456a5eb0c162a8686f748 to your computer and use it in GitHub Desktop.
c++ provides out-of-the box profiler for a piece of code in {}
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
#include <chrono> | |
#include <iostream> | |
#include <string> | |
using namespace std::chrono; | |
class LogDuration { | |
public: | |
explicit LogDuration(const string& msg = "") | |
: message(msg + ": ") | |
, start(steady_clock::now()) | |
{ | |
} | |
~LogDuration() { | |
auto finish = steady_clock::now(); | |
auto dur = finish - start; | |
cerr << message | |
<< duration_cast<microseconds>(dur).count() | |
<< " microseconds" << endl; | |
} | |
private: | |
string message; | |
steady_clock::time_point start; | |
}; | |
#define UNIQ_ID_IMPL(lineno) _a_local_var_##lineno | |
#define UNIQ_ID(lineno) UNIQ_ID_IMPL(lineno) | |
#define LOG_DURATION(message) \ | |
LogDuration UNIQ_ID(__LINE__){message}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment