chevengur

СПРИНТ №1 | Базовые алгоритмы | Урок 7: Встроенные алгоритмы

Sep 11th, 2023 (edited)
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <vector>
  4. #include <string>
  5. #include <algorithm>
  6. #include <chrono>
  7.  
  8. using namespace std;
  9.  
  10. const int SAMPLE_COUNT = 5;
  11.  
  12. vector<int> CountNames(const set<string>& storage, const vector<string>& candidates) {
  13.     vector<int> output;
  14.     for (auto& name: candidates) {
  15.         output.push_back(storage.count(name));
  16.     }
  17.     return output;
  18. }
  19.  
  20. vector<int> CountNamesLong(const set<string>& storage, const vector<string>& candidates) {
  21.     vector<int> output;
  22.     for (auto& name: candidates) {
  23.         output.push_back(count(storage.begin(), storage.end(), name));
  24.     }
  25.     return output;
  26. }
  27.  
  28. int main() {
  29.     set<string> s;
  30.     vector<string> v;
  31.     string stra = ""s;
  32.     string strb = ""s;
  33.  
  34.     for (int j = 0; j < 10000; ++j) {
  35.         s.insert(stra);
  36.         stra += "a"s;
  37.         if (j % 2 == 0) {
  38.             v.push_back(strb);
  39.             strb += "b"s;
  40.         } else {
  41.             v.push_back(stra);
  42.         }
  43.     }
  44.     cout << "Testing fast version"s << endl;
  45.     for (int i = 0; i < SAMPLE_COUNT; ++i) {
  46.         auto begin = chrono::steady_clock::now();
  47.         CountNames(s, v);
  48.         auto end = chrono::steady_clock::now();
  49.         cout << "Time difference = "s << chrono::duration_cast<chrono::microseconds>(end - begin).count() << "[µs]"s << endl;
  50.     }
  51.     cout << "Testing slow version"s << endl;
  52.     for (int i = 0; i < SAMPLE_COUNT; ++i) {
  53.         auto begin = chrono::steady_clock::now();
  54.         CountNamesLong(s, v);
  55.         auto end = chrono::steady_clock::now();
  56.         cout << "Time difference Long = "s << chrono::duration_cast<chrono::microseconds>(end - begin).count() << "[µs]"s << endl;
  57.     }
  58.     return 0;
  59. }
Add Comment
Please, Sign In to add comment