From 3e9449cfbb11adb391c3f2d7c7bda70a5e6f7b93 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 21 Jun 2025 19:14:28 +0000 Subject: [PATCH] init --- .gitignore | 3 ++ CMakeLists.txt | 14 +++++++ CMakeUserPresets.json | 9 ++++ README.md | 10 +++++ bench.cc | 98 +++++++++++++++++++++++++++++++++++++++++++ conanfile.txt | 8 ++++ 6 files changed, 142 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 CMakeUserPresets.json create mode 100644 README.md create mode 100644 bench.cc create mode 100644 conanfile.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b8087a2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.cache/ +build +compile_commands.json \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..28c355f --- /dev/null +++ b/CMakeLists.txt @@ -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 +) + diff --git a/CMakeUserPresets.json b/CMakeUserPresets.json new file mode 100644 index 0000000..71aeace --- /dev/null +++ b/CMakeUserPresets.json @@ -0,0 +1,9 @@ +{ + "version": 4, + "vendor": { + "conan": {} + }, + "include": [ + "build/Release/generators/CMakePresets.json" + ] +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..356820d --- /dev/null +++ b/README.md @@ -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 +``` \ No newline at end of file diff --git a/bench.cc b/bench.cc new file mode 100644 index 0000000..6ca0106 --- /dev/null +++ b/bench.cc @@ -0,0 +1,98 @@ +#include "benchmark/benchmark.h" +#include +#include + +static thread_local std::mt19937_64 rng(std::random_device {}()); + +template , int> = 0> +std::vector prepareRandData(int size) +{ + std::uniform_int_distribution dist(1, 1000000); + std::vector result; + for (int i = 0; i < size; ++i) { + result.push_back(dist(rng)); + } + return result; +} + +template , int> = 0> +std::vector prepareRandData(int size) +{ + std::uniform_real_distribution dist(1, 1000000); + std::vector 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(arrSize); + std::vector b = prepareRandData(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(arrSize); + std::vector b = prepareRandData(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(); \ No newline at end of file diff --git a/conanfile.txt b/conanfile.txt new file mode 100644 index 0000000..649294e --- /dev/null +++ b/conanfile.txt @@ -0,0 +1,8 @@ +[requires] +benchmark/1.9.1 + +[generators] +CMakeDeps +CMakeToolchain +[layout] +cmake_layout \ No newline at end of file