Using catch2's benchmark

master
denes 5 years ago
parent 65879a7e8b
commit a3750858c9
Signed by: denes
GPG Key ID: A7D50EAD42F9FC9F

6
.gitignore vendored

@ -9,5 +9,11 @@ install_manifest.txt
compile_commands.json compile_commands.json
CTestTestfile.cmake CTestTestfile.cmake
#kdevelop
.kdev4/
build/
performance_measuring.kdev4
#binaries
a.out a.out
performance_measuring performance_measuring

@ -1,5 +1,6 @@
// Let Catch provide main(): // Let Catch provide main():
#define CATCH_CONFIG_MAIN #define CATCH_CONFIG_MAIN
#define CATCH_CONFIG_ENABLE_BENCHMARKING
#include <catch2/catch.hpp> #include <catch2/catch.hpp>
@ -17,6 +18,20 @@ std::vector<int> generate_random_numbers(int n, int min, int max)
TEST_CASE( "Simple test" ) { TEST_CASE( "Simple test" ) {
std::vector<int> v = { 1, 2, 3, 1}; std::vector<int> v = { 1, 2, 3, 1};
int r = performance_measuring::compute(v); REQUIRE( performance_measuring::min_element_and_count(v) == 2 );
REQUIRE( r == 2 ); REQUIRE( performance_measuring::sort_and_upper_bound(v) == 2 );
}
TEST_CASE("Benchmarking") {
std::vector<int> 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);
};
} }

@ -6,12 +6,20 @@
namespace performance_measuring { namespace performance_measuring {
int compute(const std::vector<int>& v) int min_element_and_count(const std::vector<int>& v)
{ {
int min = *std::min_element(v.begin(), v.end()); int min = *std::min_element(v.begin(), v.end());
return std::count(v.begin(), v.end(), min); return std::count(v.begin(), v.end(), min);
} }
int sort_and_upper_bound(const std::vector<int>& v)
{
std::vector<int> 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 #endif

Loading…
Cancel
Save