shushus

Спринт 6. Связь потоков: в поисках оптимального ввода и вывода.

Dec 25th, 2022
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. -----------------------------------------------------------------------
  2. log_duration.h
  3. -----------------------------------------------------------------------
  4. #pragma once
  5.  
  6. #include <chrono>
  7. #include <iostream>
  8.  
  9. using namespace std;
  10. using namespace chrono;
  11. using namespace literals;
  12.  
  13. #define PROFILE_CONCAT_INTERNAL(X, Y) X##Y
  14. #define PROFILE_CONCAT(X, Y) PROFILE_CONCAT_INTERNAL(X, Y)
  15. #define UNIQUE_VAR_NAME_PROFILE PROFILE_CONCAT(profileGuard, __LINE__)
  16. #define LOG_DURATION(x) LogDuration UNIQUE_VAR_NAME_PROFILE(x)
  17.  
  18. class LogDuration {
  19. public:
  20.     LogDuration(const std::string& id) : id_(id) {
  21.     }
  22.  
  23.     ~LogDuration() {
  24.         const auto end_time = steady_clock::now();
  25.         const auto dur = end_time - start_time_;
  26.         cerr << id_ << ": "s << duration_cast<milliseconds>(dur).count() << " ms"s << endl;
  27.     }
  28.  
  29. private:
  30.     const std::string id_;
  31.     const steady_clock::time_point start_time_ = steady_clock::now();
  32. };
  33. -----------------------------------------------------------------------
  34. main.cpp
  35. -----------------------------------------------------------------------
  36. #include "log_duration.h"
  37.  
  38. #include <iostream>
  39.  
  40. using namespace std;
  41.  
  42. class StreamUntier {
  43. public:
  44.     StreamUntier(istream& stream) : stream_(stream) {
  45.         tied_before_  = stream_.tie(nullptr);
  46.     }
  47.     ~StreamUntier(){
  48.         stream_.tie(tied_before_);
  49.     }
  50.  
  51. private:
  52.     ostream* tied_before_;
  53.     istream& stream_;
  54. };
  55.  
  56. int main() {
  57.     LOG_DURATION("\\n with tie"s);
  58.  
  59.     StreamUntier guard(cin);
  60.     int i;
  61.     while (cin >> i) {
  62.         cout << i * i << "\n"s;
  63.     }
  64.  
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment