Advertisement
kutuzzzov

Урок 5 Призываем макросы

Nov 1st, 2022
1,370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. // main
  2.  
  3. #include <chrono>
  4. #include <cstdlib>
  5. #include <iostream>
  6. #include <vector>
  7. #include "log_duration.h"
  8.  
  9. using namespace std;
  10.  
  11. vector<int> ReverseVector(const vector<int>& source_vector) {
  12.     vector<int> res;
  13.     for (int i : source_vector) {
  14.         res.insert(res.begin(), i);
  15.     }
  16.  
  17.     return res;
  18. }
  19.  
  20. int CountPops(const vector<int>& source_vector, int begin, int end) {
  21.     int res = 0;
  22.  
  23.     for (int i = begin; i < end; ++i) {
  24.         if (source_vector[i]) {
  25.             ++res;
  26.         }
  27.     }
  28.  
  29.     return res;
  30. }
  31.  
  32. void AppendRandom(vector<int>& v, int n) {
  33.     for (int i = 0; i < n; ++i) {
  34.         // получаем случайное число с помощью функции rand.
  35.         // с помощью (rand() % 2) получим целое число в диапазоне 0..1.
  36.         // в C++ имеются более современные генераторы случайных чисел,
  37.         // но в данном уроке не будем их касаться
  38.         v.push_back(rand() % 2);
  39.     }
  40. }
  41.  
  42. void Operate() {
  43.     LOG_DURATION("Total");
  44.    
  45.     vector<int> random_bits;
  46.     static const int N = 1 << 17;
  47.     vector<int> reversed_bits;
  48.    
  49.     {
  50.         LOG_DURATION("Append random");
  51.         AppendRandom(random_bits, N);
  52.     }
  53.    
  54.     {
  55.         LOG_DURATION("Reverse");
  56.         reversed_bits = ReverseVector(random_bits);
  57.     }
  58.    
  59.     {
  60.         LOG_DURATION("Counting");
  61.         for (int i = 1, step = 1; i <= N; i += step, step *= 2) {
  62.             double rate = CountPops(reversed_bits, 0, i) * 100. / i;
  63.             cout << "After "s << i << " bits we found "s << rate << "% pops"s << endl;
  64.         }
  65.     }
  66. }
  67.  
  68. int main() {
  69.     Operate();
  70. }
  71.  
  72. // log_duration
  73.  
  74. #pragma once
  75.  
  76. #include <chrono>
  77. #include <iostream>
  78.  
  79. #define PROFILE_CONCAT_INTERNAL(X, Y) X ## Y
  80. #define PROFILE_CONCAT(X, Y) PROFILE_CONCAT_INTERNAL(X, Y)
  81. #define UNIQUE_VAR_NAME_PROFILE PROFILE_CONCAT(profileGuard, __LINE__)
  82. #define LOG_DURATION(x) LogDuration UNIQUE_VAR_NAME_PROFILE(x)
  83.  
  84. class LogDuration {
  85. public:
  86.     // заменим имя типа std::chrono::steady_clock
  87.     // с помощью using для удобства
  88.     using Clock = std::chrono::steady_clock;
  89.  
  90.     LogDuration(const std::string& id) : id_(id) {
  91.     }
  92.    
  93.     ~LogDuration() {
  94.         using namespace std::chrono;
  95.         using namespace std::literals;
  96.  
  97.         const auto end_time = Clock::now();
  98.         const auto dur = end_time - start_time_;
  99.         std::cerr << id_ << ": "s << duration_cast<milliseconds>(dur).count() << " ms"s << std::endl;
  100.     }
  101.  
  102. private:
  103.     const Clock::time_point start_time_ = Clock::now();
  104.     const std::string id_;
  105. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement