shushus

Спринт 6. Вглубь процессора.

Dec 25th, 2022
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 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.  
  30. private:
  31.     const std::string id_;
  32.     const Clock::time_point start_time_ = Clock::now();
  33. };
  34. -----------------------------------------------------------------------
  35. main.cpp
  36. -----------------------------------------------------------------------
  37. #include "log_duration.h"
  38.  
  39. #include <iostream>
  40. #include <numeric>
  41. #include <random>
  42. #include <string>
  43. #include <vector>
  44.  
  45. using namespace std;
  46.  
  47. vector<float> ComputeAvgTemp(const vector<vector<float>>& measures) {
  48.     if (!measures.empty()){
  49.         int meass = measures[0].size();
  50.         vector<float> size(meass);
  51.         vector<float> result(meass);
  52.         vector<float> count(meass);
  53.         for (int i = 0; i < meass; ++i) {
  54.             for (int j = 0; j < measures.size(); ++j) {
  55.                 size[i] += (measures[j][i] > 0 ? 1 : 0);
  56.                 count[i] += (measures[j][i] > 0 ? measures[j][i] : 0);
  57.             }
  58.         result[i] += (size[i] > 0 ? count[i] / size[i] : 0);
  59.         }
  60.         return result;
  61.     }else{
  62.         return {};
  63.     }
  64. }
  65.  
  66. vector<float> GetRandomVector(int size) {
  67.     static mt19937 engine;
  68.     uniform_real_distribution<float> d(-100, 100);
  69.  
  70.     vector<float> res(size);
  71.     for (int i = 0; i < size; ++i) {
  72.         res[i] = d(engine);
  73.     }
  74.     return res;
  75. }
  76.  
  77. int main() {
  78.     vector<vector<float>> data;
  79.     data.reserve(5000);
  80.  
  81.     for (int i = 0; i < 5000; ++i) {
  82.         data.push_back(GetRandomVector(5000));
  83.     }
  84.  
  85.     vector<float> avg;
  86.     {
  87.         LOG_DURATION("ComputeAvgTemp"s);
  88.         avg = ComputeAvgTemp(data);
  89.     }
  90.  
  91.     cout << "Total mean: "s << accumulate(avg.begin(), avg.end(), 0.f) / avg.size() << endl;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment