From 4b968138b031ec1b898361805445b48ff7ec285e Mon Sep 17 00:00:00 2001 From: Eric Budai Date: Sat, 20 Dec 2014 11:29:53 -0500 Subject: [PATCH] c++ performance improvement using vector instead of bitset --- cpp.cpp | 86 +++++++++++++++++++++++---------------------------------- 1 file changed, 34 insertions(+), 52 deletions(-) diff --git a/cpp.cpp b/cpp.cpp index 5eae2f2..e3a3629 100644 --- a/cpp.cpp +++ b/cpp.cpp @@ -1,73 +1,55 @@ -#include +#define _POSIX_C_SOURCE 200809L +#include #include #include #include #include #include -#include using namespace std; using namespace std::chrono; struct route{ - int dest, cost; + int dest, cost; }; struct node { - vector neighbours; + vector neighbours; }; vector readPlaces(){ - ifstream text("agraph"); - int numNodes; text >> numNodes; - vector nodes(numNodes); - int node, neighbour, cost; - while (text >> node >> neighbour >> cost){ - nodes[node].neighbours.push_back(route{neighbour, cost}); - } - return nodes; + ifstream text("agraph"); + string numNodesText; + text >> numNodesText; + int numNodes = stoi(numNodesText); + vector nodes(numNodes); + string nodeS, neighbourS, costS; + while (text >> nodeS >> neighbourS >> costS){ + nodes[stoi(nodeS)].neighbours.push_back(route{ stoi(neighbourS), stoi(costS) }); + } + return nodes; } -template -int getLongestPath(const vector &nodes, const int nodeID, bitset visited){ - visited[nodeID] = true; - int max=0; - for(const route &neighbour: nodes[nodeID].neighbours){ - if (visited[neighbour.dest] == false){ - const int dist = neighbour.cost + getLongestPath(nodes, neighbour.dest, visited); - if (dist > max){ - max = dist; - } - } - } - visited[nodeID] = false; - return max; -} - -int getLongestPath(const vector &nodes) -{ - if (nodes.size() <= 16) { - return getLongestPath<16>(nodes, 0, bitset<16>()); - } else if (nodes.size() <= 256) { - return getLongestPath<256>(nodes, 0, bitset<256>()); - } else if (nodes.size() <= 4096) { - return getLongestPath<4096>(nodes, 0, bitset<4096>()); - } else if (nodes.size() <= 65536) { - return getLongestPath<65536>(nodes, 0, bitset<65536>()); - } else if (nodes.size() <= 1048576) { - return getLongestPath<1048576>(nodes, 0, bitset<1048576>()); - } else if (nodes.size() <= 16777216) { - return getLongestPath<16777216>(nodes, 0, bitset<16777216>()); - } else { - return -1; - } +inline int getLongestPath(vector &nodes, int nodeID, vector &visited){ + visited[nodeID] = 1; + int max = 0; + for (const route& neighbour : nodes[nodeID].neighbours){ + if (visited[neighbour.dest] == 0){ + int dist = neighbour.cost + getLongestPath(nodes, neighbour.dest, visited); + if (dist > max){ + max = dist; + } + } + } + visited[nodeID] = 0; + return max; } int main(int argc, char** argv){ - auto nodes = readPlaces(); - auto start = high_resolution_clock::now(); - int len = getLongestPath(nodes); - auto end = high_resolution_clock::now(); - auto duration = (int)(0.001 * duration_cast(end - start).count()); - cout << len << " LANGUAGE C++ " << duration << std::endl; -} + auto nodes = readPlaces(); + vector visited(nodes.size(), 0); + auto start = high_resolution_clock::now(); + int len = getLongestPath(nodes, 0, visited); + auto duration = high_resolution_clock::now() - start; + cout << len << " LANGUAGE C++ " << duration_cast(duration).count() << "\n"; +} \ No newline at end of file