From 3b535f0ecba834ba2b189e68b13316ec0d5f8fa6 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Tue, 1 Aug 2023 15:24:27 +0200 Subject: A4: WIP --- a4/list_vs_vector.cc | 94 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 64 insertions(+), 30 deletions(-) (limited to 'a4/list_vs_vector.cc') diff --git a/a4/list_vs_vector.cc b/a4/list_vs_vector.cc index 1665e3e..22e40b7 100644 --- a/a4/list_vs_vector.cc +++ b/a4/list_vs_vector.cc @@ -91,24 +91,72 @@ void remove_index(C& container, int index) container.erase(it); } +struct PaddedInt +{ + PaddedInt(int i) : i(i) {} + int operator=(int other) + { + i = other; + return i; + } + + bool operator<(int other) const + { + return i < other; + } + + bool operator<(const PaddedInt& other) const + { + return i < other.i; + } + + int i; + char padding[1024 - sizeof(int)]{}; // pad up to 1k +}; + +std::ostream& operator<<(std::ostream& s, const PaddedInt& i) +{ + s << i.i; + return s; +} + +template + requires std::is_same>::value || + std::is_same>::value || + std::is_same>::value || + std::is_same>::value || + std::is_same>::value || + std::is_same>::value +std::ostream& operator<<(std::ostream& s, const C& container) +{ + for(const auto& item : container) + { + s << item << " "; + } + return s; +} + int main() { + //using experiment_type_t = int; + using experiment_type_t = PaddedInt; + Timer timer; - Octave ins_oct("insert"); + Octave ins_oct("insert-padded"); ins_oct.setAxis("N", "time (ms)"); auto& ins_plot_v = ins_oct.add("std::vector"); auto& ins_plot_l = ins_oct.add("std::list"); auto& ins_plot_s = ins_oct.add("std::set"); - Octave rem_oct("remove"); + Octave rem_oct("remove-padded"); rem_oct.setAxis("N", "time (ms)"); auto& rem_plot_v = rem_oct.add("std::vector"); auto& rem_plot_l = rem_oct.add("std::list"); auto& rem_plot_s = rem_oct.add("std::set"); - constexpr std::size_t Ns[]{10, 50, 100, 200, 500, 1000, 5000, 10'000, 15'000, 20'000, 25'000}; + constexpr std::size_t Ns[]{10, 50, 100, 200, 500, 1000, 2000, 3000, 4000, 5000, 8000, 10'000, 12'000, 14'000, 16'000, 18'000, 20'000}; for(auto N : Ns) { @@ -122,6 +170,7 @@ int main() for(int i = 0; i < 3; ++i) { + std::cout << "--- Iteration " << i << " of 3\n"; // Create N unique values in rnd vector: std::vector rnd; auto gen = get_random(i, 0, int(N) * 10); @@ -134,6 +183,11 @@ int main() } } + if(N < 64) + { + std::cout << "rnd: " << rnd << '\n'; + } + // Create vector of removal indices std::vector rem; { @@ -147,18 +201,13 @@ int main() if(N < 64) { - std::cout << "rem:"; - for(auto i : rem) - { - std::cout << " " << i; - } - std::cout << '\n'; + std::cout << "rem: " << rem << '\n'; } } // std::vector experiment { - std::vector vec; + std::vector vec; std::cout << "std::vector insert (N=" + std::to_string(N) + "):\n"; { Measure _(timer); @@ -171,12 +220,7 @@ int main() if(N < 64) { - std::cout << "vec:"; - for(auto i : vec) - { - std::cout << " " << i; - } - std::cout << '\n'; + std::cout << "vec: " << vec << '\n'; } std::cout << "std::vector remove (N=" + std::to_string(N) + "):\n"; @@ -192,7 +236,7 @@ int main() // std::list experiment { - std::list lst; + std::list lst; std::cout << "std::list insert (N=" + std::to_string(N) + "):\n"; { Measure _(timer); @@ -205,12 +249,7 @@ int main() if(N < 64) { - std::cout << "lst:"; - for(auto i : lst) - { - std::cout << " " << i; - } - std::cout << '\n'; + std::cout << "lst: " << lst << '\n'; } std::cout << "std::list remove (N=" + std::to_string(N) + "):\n"; @@ -226,7 +265,7 @@ int main() // std::set experiment { - std::set set; + std::set set; std::cout << "std::set insert (N=" + std::to_string(N) + "):\n"; { Measure _(timer); @@ -239,12 +278,7 @@ int main() if(N < 64) { - std::cout << "set:"; - for(auto i : set) - { - std::cout << " " << i; - } - std::cout << '\n'; + std::cout << "set: " << set << '\n'; } std::cout << "std::set remove (N=" + std::to_string(N) + "):\n"; -- cgit v1.2.3