Advertisement
chevengur

СПРИНТ № 6 | Профилируем и ускоряем | Урок 7: Проверяем, всё ли ускорили

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