shushus

Спринт 6. Упрощаем логирование.

Dec 25th, 2022
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. -----------------------------------------------------------------------
  2. main.cpp
  3. -----------------------------------------------------------------------
  4. #include <chrono>
  5. #include <cstdlib>
  6. #include <iostream>
  7. #include <vector>
  8. #include "log_duration.h"
  9.  
  10. using namespace std;
  11.  
  12. vector<int> ReverseVector(const vector<int>& source_vector) {
  13.     vector<int> res;
  14.     for (int i : source_vector) {
  15.         res.insert(res.begin(), i);
  16.     }
  17.     return res;
  18. }
  19.  
  20. int CountPops(const vector<int>& source_vector, int begin, int end) {
  21.     int res = 0;
  22.     for (int i = begin; i < end; ++i) {
  23.         if (source_vector[i]) {
  24.             ++res;
  25.         }
  26.     }
  27.     return res;
  28. }
  29.  
  30. void AppendRandom(vector<int>& v, int n) {
  31.     for (int i = 0; i < n; ++i) {
  32.         v.push_back(rand() % 2);
  33.     }
  34. }
  35.  
  36. void Operate() {
  37.     {
  38.         LogDuration logduration("Total"s);
  39.         vector<int> random_bits;
  40.         static const int N = 1 << 17;
  41.         {
  42.             LogDuration logduration("Append random"s);
  43.             AppendRandom(random_bits, N);
  44.         }
  45.  
  46.         vector<int> reversed_bits;
  47.         {
  48.             LogDuration logduration("Reverse"s);
  49.             reversed_bits = ReverseVector(random_bits);
  50.         }
  51.  
  52.         {
  53.             LogDuration logduration("Counting"s);
  54.             for (int i = 1, step = 1; i <= N; i += step, step *= 2) {
  55.                 double rate = CountPops(reversed_bits, 0, i) * 100. / i;
  56.                 cout << "After "s << i << " bits we found "s << rate << "% pops"s << endl;
  57.             }
  58.         }
  59.     }
  60. }
  61.  
  62. int main() {
  63.     Operate();
  64. }
  65. -----------------------------------------------------------------------
  66. log_duration.h
  67. -----------------------------------------------------------------------
  68. #pragma once
  69.  
  70. #include <chrono>
  71. #include <iostream>
  72.  
  73. class LogDuration {
  74. public:
  75.     using Clock = std::chrono::steady_clock;
  76.     std::string operation_name;
  77.  
  78.     LogDuration(std::string operation_name_) : operation_name(operation_name_){}
  79.  
  80.     ~LogDuration() {
  81.         using namespace std::chrono;
  82.         using namespace std::literals;
  83.  
  84.         const auto end_time = Clock::now();
  85.         const auto dur = end_time - start_time_;
  86.         std::cerr << operation_name << ": "s << duration_cast<milliseconds>(dur).count() << " ms"s << std::endl;
  87.     }
  88. private:
  89.     const Clock::time_point start_time_ = Clock::now();
  90. };
Advertisement
Add Comment
Please, Sign In to add comment