Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // oop2-lab3.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <algorithm>
- #include <chrono>
- #include <deque>
- #include <functional>
- #include <iostream>
- #include <list>
- #include <map>
- #include <random>
- #include <vector>
- using namespace std;
- using namespace std::chrono;
- typedef time_point<system_clock, microseconds> timepoint;
- random_device rd;
- mt19937 mers_engine(rd());
- uniform_int_distribution<int> dist;
- long long time_elapsed(timepoint begin, timepoint end) {
- return end.time_since_epoch().count() - begin.time_since_epoch().count();
- }
- int main()
- {
- const int size = 102400;
- deque<int> d(size);
- list<int> l;
- map<int, int> m;
- vector<int> v(size);
- /*auto gen = std::bind(dist, mers_engine);
- generate(begin(d), end(d), gen);
- generate(begin(v1), end(v1), gen);*/
- for (int i = 0; i < size; i++) {
- d.at(i) = i;
- l.push_back(i);
- v.at(i) = i;
- m.emplace(i, i);
- /*l.push_back(v1.at(i));
- m.emplace(v1.at(i), v1.at(i));*/
- }
- cout << "containers initialized" << endl;
- timepoint seq_d_begin = time_point_cast<microseconds>(system_clock::now());
- for_each(begin(d), end(d), [](int &i) {i++; });
- timepoint seq_d_end = time_point_cast<microseconds>(system_clock::now());
- cout << "sequential access - deque: " << time_elapsed(seq_d_begin, seq_d_end) << " microseconds" << endl;
- timepoint seq_l_begin = time_point_cast<microseconds>(system_clock::now());
- for_each(begin(l), end(l), [](int &i) {i++; });
- timepoint seq_l_end = time_point_cast<microseconds>(system_clock::now());
- cout << "sequential access - list: " << time_elapsed(seq_l_begin, seq_l_end) << " microseconds" << endl;
- timepoint seq_m_begin = time_point_cast<microseconds>(system_clock::now());
- for_each(begin(m), end(m), [](pair<const int, int> &i) {i.second++; });
- timepoint seq_m_end = time_point_cast<microseconds>(system_clock::now());
- cout << "sequential access - map: " << time_elapsed(seq_m_begin, seq_m_end) << " microseconds" << endl;
- timepoint seq_v_begin = time_point_cast<microseconds>(system_clock::now());
- for_each(begin(v), end(v), [](int &i) {i++; });
- timepoint seq_v_end = time_point_cast<microseconds>(system_clock::now());
- cout << "sequential access - vector: " << time_elapsed(seq_v_begin, seq_v_end) << " microseconds" << endl;
- timepoint rand_d_begin = time_point_cast<microseconds>(system_clock::now());
- for (int i = 0; i < size / 2; -1 * i, i++)
- d.at(i + size / 2)++;
- timepoint rand_d_end = time_point_cast<microseconds>(system_clock::now());
- cout << "random access - deque: " << time_elapsed(rand_d_begin, rand_d_end) << " microseconds" << endl;
- cout << "random access - list: no random access function" << endl;
- timepoint rand_m_begin = time_point_cast<microseconds>(system_clock::now());
- for (int i = 0; i < size / 2; -1 * i, i++)
- m.at(i + size / 2)++;
- timepoint rand_m_end = time_point_cast<microseconds>(system_clock::now());
- cout << "random access - map: " << time_elapsed(rand_m_begin, rand_m_end) << " microseconds" << endl;
- timepoint rand_v_begin = time_point_cast<microseconds>(system_clock::now());
- for (int i = 0; i < size / 2; -1 * i, i++)
- v.at(i + size / 2)++;
- timepoint rand_v_end = time_point_cast<microseconds>(system_clock::now());
- cout << "random access - vector: " << time_elapsed(rand_v_begin, rand_v_end) << " microseconds" << endl;
- timepoint ins_d_begin = time_point_cast<microseconds>(system_clock::now());
- d.insert(d.begin() + size / 2, v.at(0));
- timepoint ins_d_end = time_point_cast<microseconds>(system_clock::now());
- cout << "insert into the middle - deque: " << time_elapsed(ins_d_begin, ins_d_end) << " microseconds per element" << endl;
- auto it_l = l.begin();
- advance(it_l, distance(l.begin(), l.end()) / 2);
- timepoint ins_l_begin = time_point_cast<microseconds>(system_clock::now());
- for (int i = 0; i < size; i++)
- l.insert(it_l, v.at(i));
- timepoint ins_l_end = time_point_cast<microseconds>(system_clock::now());
- cout << "insert into the middle - list: " << time_elapsed(ins_l_begin, ins_l_end) << " microseconds" << endl;
- auto it_m = m.begin();
- advance(it_m, distance(m.begin(), m.end()) / 2);
- timepoint ins_m_begin = time_point_cast<microseconds>(system_clock::now());
- for (int i = 0; i < size; i++)
- m.insert(it_m, pair<int, int>(v.at(i), v.at(i)));
- timepoint ins_m_end = time_point_cast<microseconds>(system_clock::now());
- cout << "insert into the middle - map: " << time_elapsed(ins_m_begin, ins_m_end) << " microseconds" << endl;
- timepoint ins_v_begin = time_point_cast<microseconds>(system_clock::now());
- for (int i = 0; i < size; i++)
- v.insert(v.begin() + size / 2, v.at(i));
- timepoint ins_v_end = time_point_cast<microseconds>(system_clock::now());
- cout << "insert into the middle - vector: " << time_elapsed(ins_v_begin, ins_v_end) << " microseconds" << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement