mihaild

except test (habr)

Aug 22nd, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.33 KB | None | 0 0
  1. #include <ctime>
  2. #include <iostream>
  3. #include <vector>
  4. #include <unordered_set>
  5. #include <set>
  6. #include <cstdlib>
  7. #include <algorithm>
  8. #include <bitset>
  9. #include <climits>
  10.  
  11. using namespace std;
  12.  
  13. int sort_second(vector<int> a, vector<int> b) {
  14.     sort(b.begin(), b.end());
  15.     int result = 0;
  16.     for (const auto &i : a) {
  17.         result += binary_search(b.begin(), b.end(), i);
  18.     }
  19.     return result;
  20. }
  21.  
  22. int sort_both(vector<int> a, vector<int> b) {
  23.     sort(a.begin(), a.end());
  24.     sort(b.begin(), b.end());
  25.     int result = 0;
  26.     for (vector<int>::const_iterator ap = a.begin(), bp = b.begin(); ap != a.end() && bp != b.end(); ) {
  27.         if (*ap > *bp) {
  28.             ++bp;
  29.         }
  30.         else {
  31.             if (*bp > *ap) {
  32.                 ++ap;
  33.             }
  34.             else {
  35.                 ++result;
  36.                 ++ap;
  37.                 ++bp;
  38.             }
  39.         }
  40.     }
  41.     return result;
  42. }
  43.  
  44. int std_set(vector<int> a, vector<int> b) {
  45.     set<int> s(b.begin(), b.end());
  46.     int result = 0;
  47.     for (const auto &i : a) {
  48.         result += s.count(i);
  49.     }
  50.     return result;
  51. }
  52.  
  53. int std_unordered_set(vector<int> a, vector<int> b) {
  54.     unordered_set<int> s(b.begin(), b.end());
  55.     int result = 0;
  56.     for (const auto &i : a) {
  57.         result += s.count(i);
  58.     }
  59.     return result;
  60. }
  61.  
  62. int std_bitset(vector<int> a, vector<int> b) {
  63.     bitset<INT_MAX> *bs = new bitset<INT_MAX>;
  64.     for (const auto &i : b) {
  65.         (*bs)[i] = true;
  66.     }
  67.     int result = 0;
  68.     for (const auto &i : a) {
  69.         result += !((*bs)[i]);
  70.     }
  71.     delete bs;
  72.     return result;
  73. }
  74.  
  75. const int ROUNDS = 5;
  76.  
  77. vector<double> test(const vector<int> &a, const vector<int> &b, const vector<int> &tests, int f(vector<int> a, vector<int> b)) {
  78.     vector<double> result;
  79.     result.reserve(tests.size());
  80.     for (const auto &test : tests) {
  81.         vector<int> _a(a.begin(), a.begin() + test), _b(b.begin(), b.begin() + test);
  82.         clock_t time = clock();
  83.         int r(0);
  84.         for (int i = 0; i < ROUNDS; ++i) {
  85.             r += f(_a, _b);
  86.         }
  87.         time = clock() - time;
  88.         result.push_back(static_cast<double> (time) / CLOCKS_PER_SEC / ROUNDS);
  89.         cerr << test << ' ' << r << ' ' << result.back() << '\n';
  90.     }
  91.     return result;
  92. }
  93.  
  94. int main(void) {
  95.     const int N = 10000000;
  96.     vector<int> a(N), b(N);
  97.  
  98.     vector<int> tests;
  99.  
  100.     for (int i = 0; i < N; ++i) {
  101.         a[i] = rand();
  102.         b[i] = rand();
  103.     }
  104.     for (int i = 1; i < N; i *= 2) {
  105.         tests.push_back(i);
  106.     }
  107.    
  108.     vector<double> r;
  109.  
  110.     cout << "sort_second\n";
  111.     r = test(a, b, tests, sort_second);
  112.     for (auto i: r) {
  113.         cout << i << ' ';
  114.     }
  115.     cout << '\n';
  116.  
  117.     cout << "sort_both\n";
  118.     r = test(a, b, tests, sort_both);
  119.     for (auto i: r) {
  120.         cout << i << ' ';
  121.     }
  122.     cout << '\n';
  123.  
  124.     cout << "std_set\n";
  125.     r = test(a, b, tests, std_set);
  126.     for (auto i: r) {
  127.         cout << i << ' ';
  128.     }
  129.     cout << '\n';
  130.  
  131.     cout << "std_unordered_set\n";
  132.     r = test(a, b, tests, std_unordered_set);
  133.     for (auto i: r) {
  134.         cout << i << ' ';
  135.     }
  136.     cout << '\n';
  137.  
  138.     cout << "std_bitset\n";
  139.     r = test(a, b, tests, std_bitset);
  140.     for (auto i: r) {
  141.         cout << i << ' ';
  142.     }
  143.     cout << '\n';
  144.  
  145.     return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment