From 2a303038f117210486e68bffe1f4a61934bb1c47 Mon Sep 17 00:00:00 2001 From: ZigRazor Date: Fri, 25 Mar 2022 15:44:08 +0000 Subject: [PATCH] Created BFS benchmark Enhanced getAdjMatrix function --- benchmark/BFS_BM.cpp | 24 ++++++++++++++++++++++++ benchmark/results/README.md | 17 +++++++++++++++++ include/Graph/Graph.hpp | 17 +++++++++++------ 3 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 benchmark/BFS_BM.cpp create mode 100644 benchmark/results/README.md diff --git a/benchmark/BFS_BM.cpp b/benchmark/BFS_BM.cpp new file mode 100644 index 000000000..cb40f6973 --- /dev/null +++ b/benchmark/BFS_BM.cpp @@ -0,0 +1,24 @@ +#include +#include "CXXGraph.hpp" +#include "Utilities.hpp" + +static auto nodes = generateRandomNodes(100000, 2); +static auto edges = generateRandomEdges(100000, nodes); + +static void BFS_X(benchmark::State &state) +{ + CXXGRAPH::Graph g; + auto range_start = edges.begin(); + auto range_end = edges.find(state.range(0)); + std::unordered_map *> edgesX; + edgesX.insert(range_start, range_end); + for (auto e : edgesX) + { + g.addEdge(&(*e.second)); + } + for (auto _ : state) + { + auto &result = g.breadth_first_search(*(range_start->second->getNodePair().first)); + } +} +BENCHMARK(BFS_X)->RangeMultiplier(16)->Range((unsigned long)1, (unsigned long)1 << 12); \ No newline at end of file diff --git a/benchmark/results/README.md b/benchmark/results/README.md new file mode 100644 index 000000000..121e2a455 --- /dev/null +++ b/benchmark/results/README.md @@ -0,0 +1,17 @@ +# BENCHMARK RESULTS + +- To create Json file with results, run: + + ```bash + ./benchmark --benchmark_out= --benchmark_out_format=json --benchmark_repetitions=20 + ``` + +- To compare results, run: + + ```bash + ./compare.py benchmarks /workspaces/CXXGraph/benchmark/results/file1.json /workspaces/CXXGraph/benchmark/results/file2.json + ``` + +BFS_X/256_mean 2376804 ns 2370768 ns 20 +BFS_X/16_mean 14632 ns 14623 ns 20 +BFS_X/1_mean 1504 ns 1503 ns 20 \ No newline at end of file diff --git a/include/Graph/Graph.hpp b/include/Graph/Graph.hpp index c9c24f54c..ce1b98ada 100644 --- a/include/Graph/Graph.hpp +++ b/include/Graph/Graph.hpp @@ -80,7 +80,7 @@ namespace CXXGRAPH { private: std::deque *> edgeSet = {}; - void addElementToAdjMatrix(AdjacencyMatrix &adjMatrix, const Node *nodeFrom, const Node *nodeTo, const Edge *edge) const; + void _addElementToAdjMatrix(AdjacencyMatrix &adjMatrix, const Node *nodeFrom, const Node *nodeTo, const Edge *edge) const; std::optional> getExtenstionAndSeparator(InputOutputFormat format) const; int writeToStandardFile(const std::string &workingDir, const std::string &OFileName, bool compress, bool writeNodeFeat, bool writeEdgeWeight, InputOutputFormat format) const; int readFromStandardFile(const std::string &workingDir, const std::string &OFileName, bool compress, bool readNodeFeat, bool readEdgeWeight, InputOutputFormat format); @@ -530,7 +530,7 @@ namespace CXXGRAPH } template - void Graph::addElementToAdjMatrix(AdjacencyMatrix &adjMatrix, const Node *nodeFrom, const Node *nodeTo, const Edge *edge) const + void Graph::_addElementToAdjMatrix(AdjacencyMatrix &adjMatrix, const Node *nodeFrom, const Node *nodeTo, const Edge *edge) const { std::pair *, const Edge *> elem = {nodeTo, edge}; adjMatrix[nodeFrom].push_back(elem); @@ -938,20 +938,25 @@ namespace CXXGRAPH template const AdjacencyMatrix Graph::getAdjMatrix() const { + AdjacencyMatrix adj; + auto addElementToAdjMatrix = [&adj](const Node *nodeFrom, const Node *nodeTo, const Edge *edge){ + std::pair *, const Edge *> elem = {nodeTo, edge}; + adj[nodeFrom].push_back(std::move(elem)); + }; for (const auto &edgeSetIt : edgeSet) { if (edgeSetIt->isDirected().has_value() && edgeSetIt->isDirected().value()) { const DirectedEdge *d_edge = dynamic_cast *>(edgeSetIt); - addElementToAdjMatrix(adj, &(d_edge->getFrom()), &(d_edge->getTo()), d_edge); + addElementToAdjMatrix(&(d_edge->getFrom()), &(d_edge->getTo()), d_edge); } else if (edgeSetIt->isDirected().has_value() && !edgeSetIt->isDirected().value()) { const UndirectedEdge *ud_edge = dynamic_cast *>(edgeSetIt); ; - addElementToAdjMatrix(adj, &(ud_edge->getNode1()), &(ud_edge->getNode2()), ud_edge); - addElementToAdjMatrix(adj, &(ud_edge->getNode2()), &(ud_edge->getNode1()), ud_edge); + addElementToAdjMatrix(&(ud_edge->getNode1()), &(ud_edge->getNode2()), ud_edge); + addElementToAdjMatrix(&(ud_edge->getNode2()), &(ud_edge->getNode1()), ud_edge); } else { // is a simple edge we cannot create adj matrix @@ -1535,7 +1540,7 @@ namespace CXXGRAPH { return visited; } - const AdjacencyMatrix adj = Graph::getAdjMatrix(); + const AdjacencyMatrix &adj = Graph::getAdjMatrix(); // queue that stores vertices that need to be further explored std::queue *> tracker;