shushus

Спринт 6. Измеряем и ускоряем.

Dec 25th, 2022
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 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) : id_(id) {
  19.     }
  20.  
  21.     ~LogDuration() {
  22.         using namespace std::chrono;
  23.         using namespace std::literals;
  24.  
  25.         const auto end_time = Clock::now();
  26.         const auto dur = end_time - start_time_;
  27.         std::cerr << id_ << ": "s << duration_cast<milliseconds>(dur).count() << " ms"s << std::endl;
  28.     }
  29. private:
  30.     const std::string id_;
  31.     const Clock::time_point start_time_ = Clock::now();
  32. };
  33. -----------------------------------------------------------------------
  34. main.cpp
  35. -----------------------------------------------------------------------
  36. #include "log_duration.h"
  37.  
  38. #include <algorithm>
  39. #include <chrono>
  40. #include <cstdlib>
  41. #include <iostream>
  42. #include <vector>
  43.  
  44. using namespace std;
  45.  
  46. vector<int> ReverseVector(const vector<int>& source_vector) {
  47.     vector<int> res;
  48.  
  49.     for (int i : source_vector) {
  50.         res.insert(res.begin(), i);
  51.     }
  52.  
  53.     return res;
  54. }
  55.  
  56. vector<int> ReverseVector2(const vector<int>& source_vector) {
  57.     vector<int> res;
  58.    
  59.     for (auto iterator = source_vector.rbegin(); iterator != source_vector.rend(); ++iterator) {
  60.         res.push_back(*iterator);
  61.     }
  62.  
  63.     return res;
  64. }
  65.  
  66. vector<int> ReverseVector3(const vector<int>& source_vector) {
  67.     return {source_vector.rbegin(), source_vector.rend()};
  68. }
  69.  
  70. vector<int> ReverseVector4(const vector<int>& source_vector) {
  71.     vector<int> res;
  72.     res.reserve(source_vector.size());
  73.     res = {source_vector.rbegin(), source_vector.rend()};
  74.     return res;
  75. }
  76.  
  77. void Operate() {
  78.     vector<int> rand_vector;
  79.     int n;
  80.  
  81.     cin >> n;
  82.     rand_vector.reserve(n);
  83.  
  84.     for (int i = 0; i < n; ++i) {
  85.         rand_vector.push_back(rand());
  86.     }
  87.    
  88.     if(rand_vector.size()<=100000){
  89.        
  90.         {
  91.             LOG_DURATION("Naive"s);
  92.             ReverseVector(rand_vector);
  93.         }
  94.  
  95.         {
  96.             LOG_DURATION("Good"s);
  97.             ReverseVector2(rand_vector);
  98.         }
  99.        
  100.     }else{
  101.        
  102.         {
  103.             LOG_DURATION("Good"s);
  104.             ReverseVector2(rand_vector);
  105.         }
  106.        
  107.         {
  108.             LOG_DURATION("Best"s);
  109.             ReverseVector3(rand_vector);
  110.         }
  111.  
  112.         {
  113.             LOG_DURATION("Your"s);
  114.             ReverseVector4(rand_vector);
  115.         }
  116.        
  117.     }
  118.    
  119. }
  120.  
  121. int main() {
  122.     Operate();
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment