From 3cf89e5137ea333e81c2d62716054848ea8e4be5 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Wed, 26 Jul 2023 16:22:44 +0200 Subject: A2: Add measurement exercise.. --- a2/measurement.cc | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) (limited to 'a2/measurement.cc') diff --git a/a2/measurement.cc b/a2/measurement.cc index cf90d52..c624bfa 100644 --- a/a2/measurement.cc +++ b/a2/measurement.cc @@ -1,11 +1,3 @@ -#if 0 -import ; -import ; -import ; -import ; -import ; -import ; -#else #include #include #include @@ -13,7 +5,6 @@ import ; #include #include #include -#endif class Measure { @@ -51,7 +42,7 @@ int main() using namespace std::chrono_literals; // Sanity check measurement mechanism - measure("Sanity check (sleep 100ms):", + measure("Sanity check (sleep 100ms)", []() { std::this_thread::sleep_for(100ms); @@ -63,22 +54,28 @@ int main() // * Find the 7 in the middle of a vector where all other elements are // not 7. { - std::vector v(1'000'000, 42); // one million items with the int 42 + std::vector v(10'000'000, 42); // one million items with the int 42 v[v.size() / 2] = 7; // set middle item to 7 measure("std::find 7 in the middle", [&v]() { - std::find(v.begin(), v.end(), 7); + if(std::find(v.begin(), v.end(), 7) == std::end(v)) + { + std::cout << "That was unexpected!\n"; + } }); } // * Try to find a 7 in a vector where no element is 7 { - std::vector v(1'000'000, 42); // one million items with the int 42 + std::vector v(10'000'000, 42); // one million items with the int 42 measure("std::find 7 not there", [&v]() { - std::find(v.begin(), v.end(), 7); + if(std::find(v.begin(), v.end(), 7) != std::end(v)) + { + std::cout << "That was unexpected!\n"; + } }); } @@ -88,22 +85,28 @@ int main() // * Find the x < 7 in the middle of a vector where all other elements // are >= 7. { - std::vector v(1'000'000, 42); // one million items with the int 42 + std::vector v(10'000'000, 42); // one million items with the int 42 v[v.size() / 2] = 5; // set middle item to 5 measure("std::find_if x < 7 in the middle", [&v]() { - std::find_if(v.begin(), v.end(), [](int x){ return x < 7;}); + if(std::find_if(v.begin(), v.end(), [](int x){ return x < 7;}) == std::end(v)) + { + std::cout << "That was unexpected!\n"; + } }); } // * Try to find an x < 7 in a vector where all elements are >= 7. { - std::vector v(1'000'000, 42); // one million items with the int 42 + std::vector v(10'000'000, 42); // one million items with the int 42 measure("std::find_if x < 7 not there", [&v]() { - std::find_if(v.begin(), v.end(), [](int x){ return x < 7;}); + if(std::find_if(v.begin(), v.end(), [](int x){ return x < 7;}) != std::end(v)) + { + std::cout << "That was unexpected!\n"; + } }); } @@ -116,7 +119,7 @@ int main() std::uniform_int_distribution distrib('A', 'Z'); std::vector vs; - vs.resize(1'000'000); + vs.resize(10'000'000); for(auto &str: vs) { str.reserve(20); -- cgit v1.2.3