Skip to content

Commit 19f370d

Browse files
committed
Add google/benchmarks
1 parent f71df85 commit 19f370d

File tree

7 files changed

+57
-4
lines changed

7 files changed

+57
-4
lines changed

.clang-tidy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Checks: >
1111
1212
UseColor: true
1313
WarningsAsErrors: ''
14-
HeaderFilterRegex: '.*'
14+
HeaderFilterRegex: '^(include|src|tests|benchmarks)/'
1515
FormatStyle: file
1616
CheckOptions:
1717
- key: modernize-use-nullptr.NullMacros

CMakeLists.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
99
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
1010

1111
# Options
12-
option(ENABLE_TESTING "Build tests" ON)
13-
option(BUILD_APP "Build the demo application" ON)
14-
option(BUILD_DOCS "Enable documentation via Doxygen" ON)
12+
option(ENABLE_TESTING "Build tests" ON)
13+
option(ENABLE_BENCHMARKS "Build benchmarks" ON)
14+
option(BUILD_APP "Build the demo application" ON)
15+
option(BUILD_DOCS "Build documentation via Doxygen" ON)
1516

1617
# Add custom "Sanitize" build type
1718
if(CMAKE_CONFIGURATION_TYPES)
@@ -42,6 +43,10 @@ if(ENABLE_TESTING)
4243
add_subdirectory(tests)
4344
endif()
4445

46+
if(ENABLE_BENCHMARKS)
47+
add_subdirectory(benchmarks)
48+
endif()
49+
4550
# === Docs ===
4651
if(BUILD_DOCS)
4752
include(tools/Doxygen)

CMakePresets.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"binaryDir": "${sourceDir}/build/${presetName}",
1313
"cacheVariables": {
1414
"ENABLE_TESTING": "ON",
15+
"ENABLE_BENCHMARKS": "ON",
1516
"BUILD_APP": "ON",
1617
"BUILD_DOCS": "ON",
1718
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON"

benchmarks/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
include(FetchContent)
2+
3+
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "Disable benchmark library tests")
4+
5+
FetchContent_Declare(
6+
benchmark
7+
URL https://github.com/google/benchmark/archive/refs/tags/v1.9.4.zip
8+
DOWNLOAD_EXTRACT_TIMESTAMP TRUE # Prevents timestamp-related rebuild issues
9+
)
10+
11+
FetchContent_MakeAvailable(benchmark)
12+
13+
add_executable(benchmarks
14+
bench_arithmetic.cpp
15+
)
16+
17+
target_link_libraries(benchmarks
18+
PRIVATE benchmark::benchmark benchmark::benchmark_main ${PROJECT_NAME}::math
19+
)
20+
21+
enable_strict_warnings(benchmarks)

benchmarks/bench_arithmetic.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <benchmark/benchmark.h>
2+
3+
#include "math/arithmetic.hpp"
4+
5+
static void BM_Add(benchmark::State& state)
6+
{
7+
for (auto _ : state) // NOLINT
8+
{
9+
benchmark::DoNotOptimize(math::add(1, 2)); // NOLINT
10+
}
11+
}
12+
13+
static void BM_Subtract(benchmark::State& state)
14+
{
15+
for (auto _ : state) // NOLINT
16+
{
17+
benchmark::DoNotOptimize(math::subtract(5, 3)); // NOLINT
18+
}
19+
}
20+
21+
BENCHMARK(BM_Add);
22+
BENCHMARK(BM_Subtract);

cmake/tools/ClangFormat.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ if(CLANG_FORMAT_EXE)
1212
"${CMAKE_SOURCE_DIR}/tests/**/*.cpp"
1313
"${CMAKE_SOURCE_DIR}/tests/*.hpp"
1414
"${CMAKE_SOURCE_DIR}/tests/**/*.hpp"
15+
"${CMAKE_SOURCE_DIR}/benchmarks/*.cpp"
16+
"${CMAKE_SOURCE_DIR}/benchmarks/**/*.cpp"
1517
)
1618

1719
add_custom_target(clang-format

cmake/tools/ClangTidy.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ if(CLANG_TIDY_EXE)
66
"${CMAKE_SOURCE_DIR}/src/**/*.cpp"
77
"${CMAKE_SOURCE_DIR}/tests/*.cpp"
88
"${CMAKE_SOURCE_DIR}/tests/**/*.cpp"
9+
"${CMAKE_SOURCE_DIR}/benchmarks/*.cpp"
10+
"${CMAKE_SOURCE_DIR}/benchmarks/**/*.cpp"
911
)
1012

1113
add_custom_target(clang-tidy

0 commit comments

Comments
 (0)