From a3750858c9ef2e9c5b8bd10f7944a8b313cadd95 Mon Sep 17 00:00:00 2001 From: denes Date: Thu, 3 Oct 2019 17:38:59 +0200 Subject: [PATCH] Using catch2's benchmark --- .gitignore | 6 ++++++ catch2_test1.cpp | 19 +++++++++++++++++-- dummy_lib.hpp | 10 +++++++++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index e03f2e0..286ae7f 100644 --- a/.gitignore +++ b/.gitignore @@ -9,5 +9,11 @@ install_manifest.txt compile_commands.json CTestTestfile.cmake +#kdevelop +.kdev4/ +build/ +performance_measuring.kdev4 + +#binaries a.out performance_measuring diff --git a/catch2_test1.cpp b/catch2_test1.cpp index 8256d06..bd920f5 100644 --- a/catch2_test1.cpp +++ b/catch2_test1.cpp @@ -1,5 +1,6 @@ // Let Catch provide main(): #define CATCH_CONFIG_MAIN +#define CATCH_CONFIG_ENABLE_BENCHMARKING #include @@ -17,6 +18,20 @@ std::vector generate_random_numbers(int n, int min, int max) TEST_CASE( "Simple test" ) { std::vector v = { 1, 2, 3, 1}; - int r = performance_measuring::compute(v); - REQUIRE( r == 2 ); + REQUIRE( performance_measuring::min_element_and_count(v) == 2 ); + REQUIRE( performance_measuring::sort_and_upper_bound(v) == 2 ); +} + + +TEST_CASE("Benchmarking") { + + std::vector v = generate_random_numbers(10000, 0, 1000); + + BENCHMARK("min_element_and_count 10k") { + return performance_measuring::min_element_and_count(v); + }; + BENCHMARK("sort_and_upper_bound 10k") { + return performance_measuring::sort_and_upper_bound(v); + }; + } diff --git a/dummy_lib.hpp b/dummy_lib.hpp index e421ce1..e5e5642 100644 --- a/dummy_lib.hpp +++ b/dummy_lib.hpp @@ -6,12 +6,20 @@ namespace performance_measuring { -int compute(const std::vector& v) +int min_element_and_count(const std::vector& v) { int min = *std::min_element(v.begin(), v.end()); return std::count(v.begin(), v.end(), min); } +int sort_and_upper_bound(const std::vector& v) +{ + std::vector tmp(v); + std::sort(tmp.begin(), tmp.end()); + const auto p = std::upper_bound(tmp.begin(), tmp.end(), *tmp.begin()); + return std::distance(tmp.begin(), p); +} + } #endif