Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added benchmarking for DFS Cycle Detection #165

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions perf/graaflib/dfs_cycle_detection_benchmark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <benchmark/benchmark.h>
#include <graaflib/graph.h>
#include <graaflib/algorithm/cycle_detection/dfs_cycle_detection.h>

namespace {

template <typename EDGE_T, graaf::graph_type GRAPH_TYPE_V >
[[nodiscard]] std::vector<graaf::vertex_id_t> create_vertices(
graaf::graph<int, EDGE_T, GRAPH_TYPE_V>& graph, size_t n) {
std::vector<graaf::vertex_id_t> 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<size_t>(state.range(0))};

graaf::directed_graph<int, int> 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<size_t>(state.range(0))};

graaf::undirected_graph<int, int> 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);
Loading