shushus

Спринт 6. Проверяем, все ли ускорили.

Dec 25th, 2022
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. -----------------------------------------------------------------------
  2. log_duration.h
  3. -----------------------------------------------------------------------
  4. #pragma once
  5.  
  6. #include <chrono>
  7. #include <iostream>
  8.  
  9. #define PROFILE_CONCAT_INTERNAL(X, Y) X##Y
  10. #define PROFILE_CONCAT(X, Y) PROFILE_CONCAT_INTERNAL(X, Y)
  11. #define UNIQUE_VAR_NAME_PROFILE PROFILE_CONCAT(profileGuard, __LINE__)
  12. #define LOG_DURATION(x) LogDuration UNIQUE_VAR_NAME_PROFILE(x)
  13.  
  14. class LogDuration {
  15. public:
  16.     using Clock = std::chrono::steady_clock;
  17.  
  18.     LogDuration(const std::string& id)
  19.         : 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. main.cpp
  37. -----------------------------------------------------------------------
  38. #include <chrono>
  39. #include <cstdlib>
  40. #include <iostream>
  41. #include <vector>
  42.  
  43. #include "log_duration.h"
  44.  
  45. using namespace std;
  46.  
  47. vector<int> ReverseVector(const vector<int>& source_vector) {
  48.     return {source_vector.rbegin(), source_vector.rend()};
  49. }
  50.  
  51. int CountPops(const vector<int>& source_vector, int begin, int end) {
  52.     int res = 0;
  53.     for (int i = begin; i < end; ++i) {
  54.         if (source_vector[i]) {
  55.             ++res;
  56.         }
  57.     }
  58.     return res;
  59. }
  60.  
  61. void AppendRandom(vector<int>& v, int n) {
  62.     v.reserve(v.size());
  63.     for (int i = 0; i < n; i += 15) {
  64.         int number = rand();
  65.         int count = min(15, n - i);
  66.  
  67.         for (int j = 0; j < count; ++j) {
  68.             v.push_back((number >> j) % 2);
  69.         }
  70.     }
  71. }
  72.  
  73. void Operate() {
  74.     LOG_DURATION("Total"s);
  75.     vector<int> random_bits;
  76.     static const int N = 1 << 17;
  77.     {
  78.         LOG_DURATION("Append random"s);
  79.         AppendRandom(random_bits, N);
  80.     }
  81.     vector<int> reversed_bits;
  82.     {
  83.         LOG_DURATION("Reverse"s);
  84.  
  85.         reversed_bits = ReverseVector(random_bits);
  86.     }
  87.  
  88.     {
  89.         LOG_DURATION("Counting"s);
  90.         for (int i = 1, step = 1; i <= N; i += step, step *= 2) {
  91.             double rate = CountPops(reversed_bits, 0, i) * 100. / i;
  92.             cout << "After "s << i << " bits we found "s << rate << "% pops"s << endl;
  93.         }
  94.     }
  95. }
  96.  
  97. int main() {
  98.     Operate();
  99. }
Advertisement
Add Comment
Please, Sign In to add comment