Advertisement
Endil

oop2-lab4

Mar 29th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.88 KB | None | 0 0
  1. // oop2-lab3.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <algorithm>
  6. #include <chrono>
  7. #include <deque>
  8. #include <functional>
  9. #include <iostream>
  10. #include <list>
  11. #include <map>
  12. #include <random>
  13. #include <vector>
  14.  
  15. using namespace std;
  16. using namespace std::chrono;
  17. typedef time_point<system_clock, microseconds> timepoint;
  18. random_device rd;
  19. mt19937 mers_engine(rd());
  20. uniform_int_distribution<int> dist;
  21.  
  22. long long time_elapsed(timepoint begin, timepoint end) {
  23.     return end.time_since_epoch().count() - begin.time_since_epoch().count();
  24. }
  25.  
  26. int main()
  27. {
  28.     const int size = 102400;
  29.     deque<int> d(size);
  30.     list<int> l;
  31.     map<int, int> m;
  32.     vector<int> v(size);
  33.     /*auto gen = std::bind(dist, mers_engine);
  34.     generate(begin(d), end(d), gen);
  35.     generate(begin(v1), end(v1), gen);*/
  36.     for (int i = 0; i < size; i++) {
  37.         d.at(i) = i;
  38.         l.push_back(i);
  39.         v.at(i) = i;
  40.         m.emplace(i, i);
  41.         /*l.push_back(v1.at(i));
  42.         m.emplace(v1.at(i), v1.at(i));*/
  43.     }
  44.     cout << "containers initialized" << endl;
  45.    
  46.     timepoint seq_d_begin = time_point_cast<microseconds>(system_clock::now());
  47.     for_each(begin(d), end(d), [](int &i) {i++; });
  48.     timepoint seq_d_end = time_point_cast<microseconds>(system_clock::now());
  49.     cout << "sequential access - deque: " << time_elapsed(seq_d_begin, seq_d_end) << " microseconds" << endl;
  50.  
  51.     timepoint seq_l_begin = time_point_cast<microseconds>(system_clock::now());
  52.     for_each(begin(l), end(l), [](int &i) {i++; });
  53.     timepoint seq_l_end = time_point_cast<microseconds>(system_clock::now());
  54.     cout << "sequential access - list: " << time_elapsed(seq_l_begin, seq_l_end) << " microseconds" << endl;
  55.  
  56.     timepoint seq_m_begin = time_point_cast<microseconds>(system_clock::now());
  57.     for_each(begin(m), end(m), [](pair<const int, int> &i) {i.second++; });
  58.     timepoint seq_m_end = time_point_cast<microseconds>(system_clock::now());
  59.     cout << "sequential access - map: " << time_elapsed(seq_m_begin, seq_m_end) << " microseconds" << endl;
  60.  
  61.     timepoint seq_v_begin = time_point_cast<microseconds>(system_clock::now());
  62.     for_each(begin(v), end(v), [](int &i) {i++; });
  63.     timepoint seq_v_end = time_point_cast<microseconds>(system_clock::now());
  64.     cout << "sequential access - vector: " << time_elapsed(seq_v_begin, seq_v_end) << " microseconds" << endl;
  65.  
  66.     timepoint rand_d_begin = time_point_cast<microseconds>(system_clock::now());
  67.     for (int i = 0; i < size / 2; -1 * i, i++)
  68.         d.at(i + size / 2)++;
  69.     timepoint rand_d_end = time_point_cast<microseconds>(system_clock::now());
  70.     cout << "random access - deque: " << time_elapsed(rand_d_begin, rand_d_end) << " microseconds" << endl;
  71.  
  72.     cout << "random access - list: no random access function" << endl;
  73.  
  74.     timepoint rand_m_begin = time_point_cast<microseconds>(system_clock::now());
  75.     for (int i = 0; i < size / 2; -1 * i, i++)
  76.         m.at(i + size / 2)++;
  77.     timepoint rand_m_end = time_point_cast<microseconds>(system_clock::now());
  78.     cout << "random access - map: " << time_elapsed(rand_m_begin, rand_m_end) << " microseconds" << endl;
  79.  
  80.     timepoint rand_v_begin = time_point_cast<microseconds>(system_clock::now());
  81.     for (int i = 0; i < size / 2; -1 * i, i++)
  82.         v.at(i + size / 2)++;
  83.     timepoint rand_v_end = time_point_cast<microseconds>(system_clock::now());
  84.     cout << "random access - vector: " << time_elapsed(rand_v_begin, rand_v_end) << " microseconds" << endl;
  85.  
  86.     timepoint ins_d_begin = time_point_cast<microseconds>(system_clock::now());
  87.     d.insert(d.begin() + size / 2, v.at(0));
  88.     timepoint ins_d_end = time_point_cast<microseconds>(system_clock::now());
  89.     cout << "insert into the middle - deque: " << time_elapsed(ins_d_begin, ins_d_end) << " microseconds per element" << endl;
  90.    
  91.     auto it_l = l.begin();
  92.     advance(it_l, distance(l.begin(), l.end()) / 2);
  93.     timepoint ins_l_begin = time_point_cast<microseconds>(system_clock::now());
  94.     for (int i = 0; i < size; i++)
  95.         l.insert(it_l, v.at(i));
  96.     timepoint ins_l_end = time_point_cast<microseconds>(system_clock::now());
  97.     cout << "insert into the middle - list: " << time_elapsed(ins_l_begin, ins_l_end) << " microseconds" << endl;
  98.  
  99.     auto it_m = m.begin();
  100.     advance(it_m, distance(m.begin(), m.end()) / 2);
  101.     timepoint ins_m_begin = time_point_cast<microseconds>(system_clock::now());
  102.     for (int i = 0; i < size; i++)
  103.         m.insert(it_m, pair<int, int>(v.at(i), v.at(i)));
  104.     timepoint ins_m_end = time_point_cast<microseconds>(system_clock::now());
  105.     cout << "insert into the middle - map: " << time_elapsed(ins_m_begin, ins_m_end) << " microseconds" << endl;
  106.  
  107.     timepoint ins_v_begin = time_point_cast<microseconds>(system_clock::now());
  108.     for (int i = 0; i < size; i++)
  109.         v.insert(v.begin() + size / 2, v.at(i));
  110.     timepoint ins_v_end = time_point_cast<microseconds>(system_clock::now());
  111.     cout << "insert into the middle - vector: " << time_elapsed(ins_v_begin, ins_v_end) << " microseconds" << endl;
  112.  
  113.     system("pause");
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement