From d32595216cc5a39787e4f074bd1039036617d54b Mon Sep 17 00:00:00 2001 From: Kimberly Swanson Date: Wed, 22 May 2019 21:21:19 +0100 Subject: [PATCH] include Google Benchmark submodule, and add a benchmark --- .gitmodules | 3 +++ CMakeLists.txt | 7 +++++++ bench/CMakeLists.txt | 18 ++++++++++++++++++ bench/addition_bench.cpp | 19 +++++++++++++++++++ bench/benchmark | 1 + bench/include/bench_helpers.hpp | 10 ++++++++++ 6 files changed, 58 insertions(+) create mode 100644 bench/CMakeLists.txt create mode 100644 bench/addition_bench.cpp create mode 160000 bench/benchmark create mode 100644 bench/include/bench_helpers.hpp diff --git a/.gitmodules b/.gitmodules index 60d7d88..6a78cf3 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "cmake-modules"] path = cmake-modules url = https://github.com/bilke/cmake-modules.git +[submodule "bench/benchmark"] + path = bench/benchmark + url = https://github.com/google/benchmark.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 1424708..5415334 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,3 +44,10 @@ set_target_properties(Boost.Real_headers PROPERTIES EXCLUDE_FROM_ALL TRUE EXCLUDE_FROM_DEFAULT_BUILD TRUE LINKER_LANGUAGE CXX) + +#Google Benchmark, turn on/off with -DBENCH=ON or OFF +find_package(Threads REQUIRED) +option(BENCH "Build benchmarks" ON) +if(BENCH) + add_subdirectory(bench) +endif() diff --git a/bench/CMakeLists.txt b/bench/CMakeLists.txt new file mode 100644 index 0000000..65015d2 --- /dev/null +++ b/bench/CMakeLists.txt @@ -0,0 +1,18 @@ +set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "Suppressing benchmark's tests" FORCE) + +add_subdirectory(benchmark) +include_directories(benchmark/include + include) + +set(bench_list + addition_bench.cpp + ) + +add_executable(main_bench ${bench_list}) + + +target_link_libraries(main_bench + benchmark + Boost.Real + ${CMAKE_THREAD_LIBS_INIT} + ) diff --git a/bench/addition_bench.cpp b/bench/addition_bench.cpp new file mode 100644 index 0000000..6cd1fa6 --- /dev/null +++ b/bench/addition_bench.cpp @@ -0,0 +1,19 @@ +#include +#include +#include +#include + +// this process uses an absurd amount of memory +// sums 10,000 reals, then evaluates. +static void BM_RealAddition10000(benchmark::State& state) { + boost::real::real a ("1.2"); + boost::real::real c ("0"); + + for (auto i : state) + for (int i = 0; i < 10000; i++) + c += a; + std::cout << c << '\n'; +} +BENCHMARK(BM_RealAddition10000); + +BENCHMARK_MAIN(); diff --git a/bench/benchmark b/bench/benchmark new file mode 160000 index 0000000..090faec --- /dev/null +++ b/bench/benchmark @@ -0,0 +1 @@ +Subproject commit 090faecb454fbd6e6e17a75ef8146acb037118d4 diff --git a/bench/include/bench_helpers.hpp b/bench/include/bench_helpers.hpp new file mode 100644 index 0000000..2043a3d --- /dev/null +++ b/bench/include/bench_helpers.hpp @@ -0,0 +1,10 @@ +#ifndef BOOST_REAL_BENCH_HELPERS_HPP +#define BOOST_REAL_BENCH_HELPERS_HPP + + +#include + +unsigned int boost::real::real::maximum_precision = 10; +unsigned int boost::real::real_algorithm::maximum_precision = 10; + +#endif //BOOST_REAL_BENCH_HELPERS_HPP