简体   繁体   中英

C++ unordered_map of Vector with >100.000 Keys

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

Here's what you can try, in that order.

  1. 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 .

  2. If you know in advance how many elements are you expecting, call reserve on your unordered_map

  3. 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.

  4. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM