shushus

Спринт 1. Встроенные алгоритмы.

Dec 3rd, 2022
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. #include <algorithm>
  2. #include <chrono>
  3. #include <iostream>
  4. #include <set>
  5. #include <string>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. const int SAMPLE_COUNT = 5;
  11.  
  12. vector<int> CountNamesLong(const set<string>& storage, const vector<string>& candidates) {
  13.     vector<int> output;
  14.     for (auto& name : candidates) {
  15.         output.push_back(count(storage.begin(), storage.end(), name));
  16.     }
  17.     return output;
  18. }
  19.  
  20. vector<int> CountNames(const set<string>& storage, const vector<string>& candidates) {
  21.     vector<int> output;
  22.     for (auto& name : candidates) {
  23.         output.push_back(storage.count(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 slow version" << 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 Long = "s
  50.              << chrono::duration_cast<chrono::microseconds>(end - begin).count() << "[µs]"s << endl;
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment