ahoy
This commit is contained in:
commit
6bd585af58
6 changed files with 163 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
.cache
|
||||||
|
build
|
13
CMakeLists.txt
Normal file
13
CMakeLists.txt
Normal file
|
@ -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)
|
9
CMakeUserPresets.json
Normal file
9
CMakeUserPresets.json
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"version": 4,
|
||||||
|
"vendor": {
|
||||||
|
"conan": {}
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"build\\Release\\generators\\CMakePresets.json"
|
||||||
|
]
|
||||||
|
}
|
18
clang_profile
Normal file
18
clang_profile
Normal file
|
@ -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
|
9
conanfile.txt
Normal file
9
conanfile.txt
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
[requires]
|
||||||
|
catch2/3.8.0
|
||||||
|
|
||||||
|
[generators]
|
||||||
|
CMakeDeps
|
||||||
|
CMakeToolchain
|
||||||
|
|
||||||
|
[layout]
|
||||||
|
cmake_layout
|
112
test.cpp
Normal file
112
test.cpp
Normal file
|
@ -0,0 +1,112 @@
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
#include <catch2/benchmark/catch_benchmark_all.hpp>
|
||||||
|
#include <cmath>
|
||||||
|
#include <random>
|
||||||
|
#define CATCH_CONFIG_MAIN
|
||||||
|
#include <catch2/catch_all.hpp>
|
||||||
|
|
||||||
|
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<Circle> 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<Circle> 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;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in a new issue