This commit is contained in:
root 2025-06-21 19:14:28 +00:00
commit 3e9449cfbb
6 changed files with 142 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.cache/
build
compile_commands.json

14
CMakeLists.txt Normal file
View File

@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.20)
project(divisionBench)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
find_package(benchmark REQUIRED)
add_executable(${PROJECT_NAME} bench.cc)
target_link_libraries(${PROJECT_NAME} PRIVATE
benchmark::benchmark_main
benchmark::benchmark
)

9
CMakeUserPresets.json Normal file
View File

@ -0,0 +1,9 @@
{
"version": 4,
"vendor": {
"conan": {}
},
"include": [
"build/Release/generators/CMakePresets.json"
]
}

10
README.md Normal file
View File

@ -0,0 +1,10 @@
## How to install & run
need to have conan installed
```bash
#assume that you have conan default profile
conan install . --build=missing
cmake --build --preset=conan-release
# or just write name of your preset instead of conan-release
```

98
bench.cc Normal file
View File

@ -0,0 +1,98 @@
#include "benchmark/benchmark.h"
#include <random>
#include <type_traits>
static thread_local std::mt19937_64 rng(std::random_device {}());
template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
std::vector<T> prepareRandData(int size)
{
std::uniform_int_distribution<T> dist(1, 1000000);
std::vector<T> result;
for (int i = 0; i < size; ++i) {
result.push_back(dist(rng));
}
return result;
}
template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
std::vector<T> prepareRandData(int size)
{
std::uniform_real_distribution<T> dist(1, 1000000);
std::vector<T> result;
for (int i = 0; i < size; ++i) {
result.push_back(dist(rng));
}
return result;
}
static constexpr int arrSize = 1 << 20;
static constexpr int bitmask = arrSize - 1;
static void int64_division(benchmark::State& state)
{
std::vector a = prepareRandData<int64_t>(arrSize);
std::vector b = prepareRandData<int64_t>(arrSize);
int64_t i = 0, i2 = 0;
for (auto _ : state) {
benchmark::DoNotOptimize(a[i++ & bitmask] / b[i2++ & bitmask]);
}
}
static void double_division(benchmark::State& state)
{
std::vector a = prepareRandData<double>(arrSize);
std::vector b = prepareRandData<double>(arrSize);
int64_t i = 0, i2 = 0;
for (auto _ : state) {
benchmark::DoNotOptimize(a[i++ & bitmask] / b[i2++ & bitmask]);
}
}
static void DoNothing(benchmark::State& state)
{
while (state.KeepRunning())
;
}
static void BM_IntDivision(benchmark::State& state)
{
volatile int a = 123456789;
volatile int b = 123;
int result;
for (auto _ : state) {
result = a / b;
benchmark::DoNotOptimize(result);
}
}
BENCHMARK(BM_IntDivision);
static void BM_FloatDivision(benchmark::State& state)
{
volatile float a = 123456789.0f;
volatile float b = 123.0f;
float result;
for (auto _ : state) {
result = a / b;
benchmark::DoNotOptimize(result);
}
}
BENCHMARK(BM_FloatDivision);
static void BM_DoubleDivision(benchmark::State& state)
{
volatile double a = 123456789.0;
volatile double b = 123.0;
double result;
for (auto _ : state) {
result = a / b;
benchmark::DoNotOptimize(result);
}
}
BENCHMARK(BM_DoubleDivision);
BENCHMARK(DoNothing);
BENCHMARK(int64_division);
BENCHMARK(double_division);
BENCHMARK_MAIN();

8
conanfile.txt Normal file
View File

@ -0,0 +1,8 @@
[requires]
benchmark/1.9.1
[generators]
CMakeDeps
CMakeToolchain
[layout]
cmake_layout