-
Notifications
You must be signed in to change notification settings - Fork 813
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example run: ``` $ tools/build_and_run_benchmark.py path_benchmark -------------------------------------------------------- Benchmark Time CPU Iterations -------------------------------------------------------- BM_SinglePath 11826 ns 11826 ns 57912 BM_Bridges 86306 ns 86301 ns 7776 BM_NoPath 147191 ns 147171 ns 4723 ```
- Loading branch information
Showing
2 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#include <cstdint> | ||
|
||
#include <benchmark/benchmark.h> | ||
#include <utility> | ||
|
||
#include "engine/path.h" | ||
#include "engine/point.hpp" | ||
#include "engine/points_in_rectangle_range.hpp" | ||
#include "engine/size.hpp" | ||
|
||
namespace devilution { | ||
namespace { | ||
|
||
struct Map { | ||
Size size; | ||
const char *data; | ||
char operator[](const Point &p) const { return data[p.y * size.width + p.x]; } | ||
}; | ||
|
||
std::pair<Point, Point> FindStartDest(const Map &m) | ||
{ | ||
Point start, dest; | ||
for (Point p : PointsInRectangle(Rectangle(Point { 0, 0 }, m.size))) { | ||
switch (m[p]) { | ||
case 'S': | ||
start = p; | ||
break; | ||
case 'E': | ||
dest = p; | ||
break; | ||
} | ||
} | ||
return { start, dest }; | ||
} | ||
|
||
void BenchmarkMap(const Map &map, benchmark::State &state) | ||
{ | ||
const auto [start, dest] = FindStartDest(map); | ||
const auto posOk = /*posOk=*/[&map](Point p) { return map[p] != '#'; }; | ||
for (auto _ : state) { | ||
int8_t path[MaxPathLength]; | ||
auto result = FindPath(/*canStep=*/[](Point, Point) { return true; }, | ||
posOk, start, dest, path); | ||
benchmark::DoNotOptimize(result); | ||
} | ||
} | ||
|
||
void BM_SinglePath(benchmark::State &state) | ||
{ | ||
BenchmarkMap( | ||
Map { | ||
Size { 15, 15 }, | ||
"###############" | ||
"#...#...#.....#" | ||
"#.#.#.#.#.###.#" | ||
"#S#...#.#.#...#" | ||
"#######.#.#.###" | ||
"##...##.#.#...#" | ||
"#######.#.###.#" | ||
"###..E#...#...#" | ||
"###.#######.###" | ||
"#...###...#...#" | ||
"#.#####.#.###.#" | ||
"#.#...#.#.#...#" | ||
"#.#.#.#.#.#.###" | ||
"#...#...#...###" | ||
"###############" }, | ||
state); | ||
} | ||
|
||
void BM_Bridges(benchmark::State &state) | ||
{ | ||
BenchmarkMap( | ||
Map { | ||
Size { 15, 15 }, | ||
"###############" | ||
"#.S...........#" | ||
"#.............#" | ||
"#.............#" | ||
"#.............#" | ||
"#.............#" | ||
"############.##" | ||
"#.............#" | ||
"#.............#" | ||
"#.............#" | ||
"#.............#" | ||
"#.............#" | ||
"###.###########" | ||
"#............E#" | ||
"###############" }, | ||
state); | ||
} | ||
|
||
void BM_NoPath(benchmark::State &state) | ||
{ | ||
BenchmarkMap( | ||
Map { | ||
Size { 15, 15 }, | ||
"###############" | ||
"#.S...........#" | ||
"#.............#" | ||
"#.............#" | ||
"#.............#" | ||
"#.............#" | ||
"#.............#" | ||
"#.............#" | ||
"#.............#" | ||
"#.............#" | ||
"#.............#" | ||
"#.............#" | ||
"###############" | ||
"#............E#" | ||
"###############" }, | ||
state); | ||
} | ||
|
||
BENCHMARK(BM_SinglePath); | ||
BENCHMARK(BM_Bridges); | ||
BENCHMARK(BM_NoPath); | ||
|
||
} // namespace | ||
} // namespace devilution |