diff --git a/perf/graaflib/dfs_cycle_detection_benchmark.cpp b/perf/graaflib/dfs_cycle_detection_benchmark.cpp new file mode 100644 index 00000000..5889813a --- /dev/null +++ b/perf/graaflib/dfs_cycle_detection_benchmark.cpp @@ -0,0 +1,64 @@ +#include +#include +#include + +namespace { + +template +[[nodiscard]] std::vector create_vertices( + graaf::graph& graph, size_t n) { + std::vector vertices{}; + vertices.reserve(n); + + for (size_t i{0}; i < n; ++i) { + vertices.push_back(graph.add_vertex(i)); + } + + return vertices; +} + +static void bm_dfs_cycle_detection_directed(benchmark::State& state) { + const auto number_of_edges{static_cast(state.range(0))}; + + graaf::directed_graph graph{}; + + // We create enough vertices to construct the requested number of edges + const auto number_of_vertices{number_of_edges + 1}; + const auto vertices{create_vertices(graph, number_of_vertices)}; + + // Linear Graph + for (size_t i{0}; i < number_of_edges; ++i) { + graph.add_edge(vertices[i], vertices[i+1], i); + } + + for (auto _ : state) { + auto result = graaf::algorithm::dfs_cycle_detection(graph); + benchmark::DoNotOptimize(result); + } +} + +static void bm_dfs_cycle_detection_undirected(benchmark::State &state) { + const auto number_of_edges{static_cast(state.range(0))}; + + graaf::undirected_graph graph{}; + + + // We create enough vertices to construct the requested number of edges + const auto number_of_vertices{number_of_edges + 1}; + const auto vertices{create_vertices(graph, number_of_vertices)}; + + // Linear Graph + for (size_t i{0}; i < number_of_edges; ++i) { + graph.add_edge(vertices[i], vertices[i+1], i); + } + + for (auto _ : state) { + auto result = graaf::algorithm::dfs_cycle_detection(graph); + benchmark::DoNotOptimize(result); + } +} +} // namespace + +// Register the benchmarks +BENCHMARK(bm_dfs_cycle_detection_directed)->Range(100, 1000); +BENCHMARK(bm_dfs_cycle_detection_undirected) -> Range(100, 1000); \ No newline at end of file