简体   繁体   English

具有100.000个键的Vector的C ++ unordered_map

[英]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 我必须阅读一个边列表,其中包含100.000个顶点和约200.000个边的形式:顶点A --->顶点B,成本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 我有一个带有int键(顶点A)的unordered_map,并将键映射到Edges的向量,其中Edges包含顶点B和Cost X,如下所示: unordered_map<int, vector<Edge> > adjList填写我的adjList是

(... 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. 用100.000个顶点和边填充adjList需要很长时间,而我的VS性能分析器告诉我,unordered_map的operator []函数是我的瓶颈。 Is the any other method to put my data into the unodre_map? 还有其他方法可以将我的数据放入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. 默认情况下,VS中的调试版本中STL集合的运行速度极慢。 You can also do same with some #defines . 您也可以对某些#defines进行相同操作。

  2. If you know in advance how many elements are you expecting, call reserve on your unordered_map 如果您事先知道需要多少个元素,请在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. 可能很小,可能已由编译器进行了优化,但仍然最好用使用vector :: emplace_back的单行替换两行,以便在该矢量内创建一个新的Edge。

  4. If that's not enough, move to better hash maps. 如果这还不够,请转到更好的哈希图。 While easy to use, portable, and standard, STL collections aren't exceptionally fast. STL集合虽然易于使用,可移植且是标准的,但并不是特别快。 On Windows I recommend CAtlMap , it's much faster. 在Windows上,我建议使用CAtlMap ,它要快得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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