diff --git a/catch2_test1.cpp b/catch2_test1.cpp index bd920f5..8bb3858 100644 --- a/catch2_test1.cpp +++ b/catch2_test1.cpp @@ -18,6 +18,7 @@ std::vector generate_random_numbers(int n, int min, int max) TEST_CASE( "Simple test" ) { std::vector v = { 1, 2, 3, 1}; + REQUIRE( performance_measuring::count_on_the_way(v) == 2 ); REQUIRE( performance_measuring::min_element_and_count(v) == 2 ); REQUIRE( performance_measuring::sort_and_upper_bound(v) == 2 ); } @@ -27,6 +28,9 @@ TEST_CASE("Benchmarking") { std::vector v = generate_random_numbers(10000, 0, 1000); + BENCHMARK("count_on_the_way 10k") { + return performance_measuring::count_on_the_way(v); + }; BENCHMARK("min_element_and_count 10k") { return performance_measuring::min_element_and_count(v); }; diff --git a/dummy_lib.hpp b/dummy_lib.hpp index e5e5642..bc62e4a 100644 --- a/dummy_lib.hpp +++ b/dummy_lib.hpp @@ -6,6 +6,23 @@ namespace performance_measuring { +int count_on_the_way(const std::vector& v) +{ + int min = v[0]; + int c = 1; + for (int i = 1; i < v.size(); ++i) { + if (v[i] > min) { + continue; + } if (v[i] == min) { + ++c; + } else { + min = v[i]; + c = 1; + } + } + return c; +} + int min_element_and_count(const std::vector& v) { int min = *std::min_element(v.begin(), v.end());