Advertisement
chevengur

СПРИНТ № 6 | Профилируем и ускоряем | Урок 5: Призываем макросы

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