mirror of
https://github.com/diasurgical/DevilutionX.git
synced 2026-05-21 05:40:35 +00:00
112e113201
Benchmark: ``` cmake -S. -Bbuild-reld -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DBUILD_TESTING=ON cmake --build build-reld --target crawl_benchmark && build-reld/crawl_benchmark ``` Before: ``` ------------------------------------------------------ Benchmark Time CPU Iterations ------------------------------------------------------ BM_Crawl/1 3.53 ns 3.53 ns 198384032 BM_Crawl/4 54.3 ns 54.2 ns 12907171 BM_Crawl/16 733 ns 732 ns 955101 BM_Crawl/20 1142 ns 1141 ns 613766 ``` After: ``` ------------------------------------------------------ Benchmark Time CPU Iterations ------------------------------------------------------ BM_Crawl/1 1.36 ns 1.36 ns 453018506 BM_Crawl/4 5.59 ns 5.59 ns 124244505 BM_Crawl/16 102 ns 101 ns 6577269 BM_Crawl/20 147 ns 147 ns 4684004 ```
48 lines
1.5 KiB
C++
48 lines
1.5 KiB
C++
#include "crawl.hpp"
|
|
|
|
#include <type_traits>
|
|
|
|
#include <function_ref.hpp>
|
|
|
|
#include "engine/displacement.hpp"
|
|
|
|
namespace devilution {
|
|
|
|
bool DoCrawl(unsigned radius, tl::function_ref<bool(Displacement)> function)
|
|
{
|
|
return DoCrawl(radius, radius, function);
|
|
}
|
|
|
|
bool DoCrawl(unsigned minRadius, unsigned maxRadius, tl::function_ref<bool(Displacement)> function)
|
|
{
|
|
for (int r = static_cast<int>(minRadius); r <= static_cast<int>(maxRadius); ++r) {
|
|
if (!function(Displacement { 0, r })) return false;
|
|
if (r == 0) continue;
|
|
if (!function(Displacement { 0, -r })) return false;
|
|
for (int x = 1; x < r; ++x) {
|
|
if (!function(Displacement { -x, r })) return false;
|
|
if (!function(Displacement { x, r })) return false;
|
|
if (!function(Displacement { -x, -r })) return false;
|
|
if (!function(Displacement { x, -r })) return false;
|
|
}
|
|
if (r > 1) {
|
|
const int d = r - 1;
|
|
if (!function(Displacement { -d, d })) return false;
|
|
if (!function(Displacement { d, d })) return false;
|
|
if (!function(Displacement { -d, -d })) return false;
|
|
if (!function(Displacement { d, -d })) return false;
|
|
}
|
|
if (!function(Displacement { -r, 0 })) return false;
|
|
if (!function(Displacement { r, 0 })) return false;
|
|
for (int y = 1; y < r; ++y) {
|
|
if (!function(Displacement { -r, y })) return false;
|
|
if (!function(Displacement { r, y })) return false;
|
|
if (!function(Displacement { -r, -y })) return false;
|
|
if (!function(Displacement { r, -y })) return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
} // namespace devilution
|