diff --git a/ex1E_vector_vs_list/main.cpp b/ex1E_vector_vs_list/main.cpp new file mode 100644 index 0000000..13ec435 --- /dev/null +++ b/ex1E_vector_vs_list/main.cpp @@ -0,0 +1,59 @@ +#include "../utils/timing.h" +#include +#include +#include +#include +#include +#include +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 x_vec = {}; + list 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; +}