Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- log_duration.h
- #pragma once
- #include <chrono>
- #include <iostream>
- using namespace std;
- using namespace chrono;
- using namespace literals;
- #define PROFILE_CONCAT_INTERNAL(X, Y) X##Y
- #define PROFILE_CONCAT(X, Y) PROFILE_CONCAT_INTERNAL(X, Y)
- #define UNIQUE_VAR_NAME_PROFILE PROFILE_CONCAT(profileGuard, __LINE__)
- #define LOG_DURATION(x) LogDuration UNIQUE_VAR_NAME_PROFILE(x)
- class LogDuration {
- public:
- LogDuration(const std::string& id) : id_(id) {
- }
- ~LogDuration() {
- const auto end_time = steady_clock::now();
- const auto dur = end_time - start_time_;
- cerr << id_ << ": "s << duration_cast<milliseconds>(dur).count() << " ms"s << endl;
- }
- private:
- const std::string id_;
- const steady_clock::time_point start_time_ = steady_clock::now();
- };
- ***************************************************************************************************************************************
- main.cpp
- #include "log_duration.h"
- #include <iostream>
- using namespace std;
- class StreamUntier {
- public:
- StreamUntier(istream& cin): stream(cin)
- {
- tied_before_ = stream.tie(nullptr);
- }
- ~StreamUntier()
- {
- stream.tie(tied_before_);
- }
- private:
- istream& stream;
- ostream* tied_before_;
- };
- int main() {
- LOG_DURATION("\\n with tie"s);
- StreamUntier guard(cin);
- int i;
- while (cin >> i) {
- cout << i * i << "\n"s;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement