Upload files to "ex1E_vector_vs_list"
This commit is contained in:
parent
9098bcc99a
commit
98908fc4b0
1 changed files with 59 additions and 0 deletions
59
ex1E_vector_vs_list/main.cpp
Normal file
59
ex1E_vector_vs_list/main.cpp
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
#include "../utils/timing.h"
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
|
||||
|
||||
size_t random_integer(int lower_bound, int upper_bound)
|
||||
{
|
||||
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
|
||||
|
||||
minstd_rand0 generator (seed);
|
||||
|
||||
return lower_bound + generator() % (upper_bound - lower_bound + 1);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
// start with generating a sorted vector/list
|
||||
size_t n = 10000;
|
||||
vector<int> x_vec = {};
|
||||
list<int> x_list = {};
|
||||
for(size_t k = 0; k < n; ++k)
|
||||
{
|
||||
x_vec.push_back(k + 1);
|
||||
x_list.push_back(k + 1);
|
||||
}
|
||||
|
||||
|
||||
// insert new random entries such that the container stays sorted
|
||||
tic();
|
||||
for(size_t i = 0; i < n; ++i)
|
||||
{
|
||||
size_t new_entry = random_integer(1,n);
|
||||
auto it = lower_bound(x_vec.begin(), x_vec.end(), new_entry);
|
||||
x_vec.insert(it, new_entry);
|
||||
}
|
||||
double time_1 = toc();
|
||||
|
||||
tic();
|
||||
for(size_t i = 0; i < n; ++i)
|
||||
{
|
||||
size_t new_entry = random_integer(1,n);
|
||||
auto it = lower_bound(x_list.begin(), x_list.end(), new_entry);
|
||||
x_list.insert(it, new_entry);
|
||||
}
|
||||
double time_2 = toc();
|
||||
|
||||
|
||||
// check results
|
||||
cout << "New vector is sorted: " << std::boolalpha << is_sorted(x_vec.begin(), x_vec.end()) << "\tsize: " << x_vec.size() << "\tduration: " << time_1 << endl;
|
||||
cout << "New list is sorted: " << std::boolalpha << is_sorted(x_list.begin(), x_list.end()) << "\tsize: " << x_list.size() << "\tduration: " << time_2 << endl;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue