commit 6bd585af58d22febd1d1b88d35397b5f3fc341b0 Author: NukeBird Date: Mon Apr 28 12:01:47 2025 +0300 ahoy diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7194ea7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.cache +build diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..cc42552 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.22.0) + +project(intersection_bench) + +set(CMAKE_CXX_FLAGS "-Wall -Wextra") +set(CMAKE_CXX_FLAGS_DEBUG "-g") +set(CMAKE_CXX_FLAGS_RELEASE "-O3") +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +find_package(Catch2 REQUIRED) + +add_executable(intersection_bench test.cpp) +target_link_libraries(intersection_bench PRIVATE Catch2::Catch2WithMain) diff --git a/CMakeUserPresets.json b/CMakeUserPresets.json new file mode 100644 index 0000000..09443ac --- /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/clang_profile b/clang_profile new file mode 100644 index 0000000..e68cc71 --- /dev/null +++ b/clang_profile @@ -0,0 +1,18 @@ +[settings] +os=Windows +arch=x86_64 +build_type=Release +compiler=clang +compiler.version=19 +compiler.cppstd=gnu20 +compiler.runtime=static +compiler.runtime_type=Release +compiler.runtime_version=v143 + +[buildenv] +CC=clang +CXX=clang +RC=clang + +[conf] +tools.cmake.cmaketoolchain:generator=Ninja diff --git a/conanfile.txt b/conanfile.txt new file mode 100644 index 0000000..30594fc --- /dev/null +++ b/conanfile.txt @@ -0,0 +1,9 @@ +[requires] +catch2/3.8.0 + +[generators] +CMakeDeps +CMakeToolchain + +[layout] +cmake_layout diff --git a/test.cpp b/test.cpp new file mode 100644 index 0000000..2fa9f98 --- /dev/null +++ b/test.cpp @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#define CATCH_CONFIG_MAIN +#include + +struct Circle +{ + float X; + float Y; + float R; +}; + +bool circle_circle_intersect(const Circle& a, const Circle& b) +{ + auto dx = a.X - b.X; + auto dy = a.Y - b.Y; + auto distance = std::sqrt(dx*dx + dy*dy); + return distance < a.R + b.R; +} + +bool circle_circle_intersect2(const Circle& a, const Circle& b) +{ + auto dx = a.X - b.X; + auto dy = a.Y - b.Y; + auto r_sum = a.R + b.R; + return dx*dx + dy*dy < r_sum * r_sum; +} + +std::random_device dev; +std::mt19937 rng(dev()); + +Circle generate_random_circle() +{ + static std::uniform_real_distribution pos_dist(-100.0f, 100.0f); + static std::uniform_real_distribution r_dist(-10.0f, 10.0f); + + return Circle + { + .X = pos_dist(rng), + .Y = pos_dist(rng), + .R = r_dist(rng) + }; +} + +TEST_CASE("Oh Hi Mark", "[test]") +{ + BENCHMARK_ADVANCED("Circle circle (sqrt) [10000]")( + Catch::Benchmark::Chronometer meter) + { + std::vector circles; + circles.reserve(10000); + + for(int i = 0; i < 10000; ++i) + { + circles.emplace_back(generate_random_circle()); + } + + meter.measure( + [&] + { + int total = 0; + + for(size_t i = 0; i < circles.size(); ++i) + { + const auto& a = circles[i]; + + for(size_t j = i + 1; j < circles.size(); ++j) + { + const auto& b = circles[j]; + auto collision = circle_circle_intersect(a, b); + total += collision; + } + } + + return total; + }); + }; + + BENCHMARK_ADVANCED("Circle circle (no sqrt) [10000]")( + Catch::Benchmark::Chronometer meter) + { + std::vector circles; + circles.reserve(10000); + + for(int i = 0; i < 10000; ++i) + { + circles.emplace_back(generate_random_circle()); + } + + meter.measure( + [&] + { + int total = 0; + + for(size_t i = 0; i < circles.size(); ++i) + { + const auto& a = circles[i]; + + for(size_t j = i + 1; j < circles.size(); ++j) + { + const auto& b = circles[j]; + auto collision = circle_circle_intersect2(a, b); + total += collision; + } + } + + return total; + }); + }; +}