Skip to content

Instantly share code, notes, and snippets.

@andreysolovyev381
Created October 4, 2019 08:18
Show Gist options
  • Save andreysolovyev381/76fbf9b41cd456a5eb0c162a8686f748 to your computer and use it in GitHub Desktop.
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 {}
#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