shushus

Спринт 6. Призываем макросы.

Dec 25th, 2022
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 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.         LOG_DURATION("Total"s);
  39.         vector<int> random_bits;
  40.         static const int N = 1 << 17;
  41.         {
  42.             LOG_DURATION("Append random"s);
  43.             AppendRandom(random_bits, N);
  44.         }
  45.  
  46.         vector<int> reversed_bits;
  47.         {
  48.             LOG_DURATION("Reverse"s);
  49.             reversed_bits = ReverseVector(random_bits);
  50.         }
  51.  
  52.         {
  53.             LOG_DURATION("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. #define PROFILE_CONCAT_INTERNAL(X, Y) X ## Y
  74. #define PROFILE_CONCAT(X, Y) PROFILE_CONCAT_INTERNAL(X, Y)
  75. #define UNIQUE_VAR_NAME_PROFILE PROFILE_CONCAT(profileGuard, __LINE__)
  76. #define LOG_DURATION(x) LogDuration UNIQUE_VAR_NAME_PROFILE(x)
  77.  
  78. class LogDuration {
  79. public:
  80.     using Clock = std::chrono::steady_clock;
  81.     std::string operation_name;
  82.  
  83.     LogDuration(std::string operation_name_) : operation_name(operation_name_){}
  84.  
  85.     ~LogDuration() {
  86.         using namespace std::chrono;
  87.         using namespace std::literals;
  88.  
  89.         const auto end_time = Clock::now();
  90.         const auto dur = end_time - start_time_;
  91.         std::cerr << operation_name << ": "s << duration_cast<milliseconds>(dur).count() << " ms"s << std::endl;
  92.     }
  93.  
  94. private:
  95.     const Clock::time_point start_time_ = Clock::now();
  96. };
Advertisement
Add Comment
Please, Sign In to add comment