Advertisement
kutuzzzov

Урок 7 Проверяем всё ли ускорили

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