Reputation: 35
I have to read a edgelist which 100.000 vertices and around 200.000 edges in form: Vertex A--->Vertex B with Cost X
I have an unordered_map with int keys (Vertex A) and map the key to a vector of Edges, where Edges contains the Vertex B and the Cost X, like this: unordered_map<int, vector<Edge> > adjList
The method i use to fill my adjList is
(... read Vertex A, B and Cost X out of file...)
Edge e(Vertex B, Cost X) //create a new Object Edge
adj[Vertex A].push_back(e); //put it into the vector
Filling the adjList
with 100.000 vertices and the edges takes a pretty long time, and my VS-Performance Profiler tells me, that operator[] function of the unordered_map is my bottleneck. Is the any other method to put my data into the unodre_map? Or even an other data-structur to use for this application?
Thanks
Upvotes: 1
Views: 482
Reputation: 21956
Here’s what you can try, in that order.
Ensure you’re building Release configuration. By default, STL collections are extremely slow in debug builds in VS. You can also do same with some #defines.
If you know in advance how many elements are you expecting, call reserve on your unordered_map
Very minor and probably optimized by the compiler, but still, you better replace your two lines with a single one that uses vector::emplace_back to create a new Edge right inside that vector.
If that’s not enough, move to better hash maps. While easy to use, portable, and standard, STL collections aren’t exceptionally fast. On Windows I recommend CAtlMap, it’s much faster.
Upvotes: 1