简体   繁体   English

我可以在没有地图执行任何旋转的情况下预加载STL地图吗?

[英]Can I preload an STL map without the map performing any rotations?

I have sorted data coming from a database to initialize an STL map. 我已经对来自数据库的数据进行了排序以初始化STL映射。 Only 5% of data will be changed later on inside the map. 以后只有5%的数据会在地图内更改。

As I understand, there will be an overhead of rotations incurred for each insertions. 据我了解,每次插入都会产生旋转开销。 Is it possible to bypass the overhead for sorted data? 是否可以绕过排序数据的开销? eg is there an option to skip rotation, and another STL algoritm to do create a balanced tree with sorted data? 例如,是否有跳过旋转的选项,另一个STL算法要创建一个带有排序数据的平衡树?

PS : I am aware there will be only 2 max rotations, but was wondering if I can improve performance further. PS:我知道最多只有2次旋转,但是想知道我是否可以进一步提高性能。

I assume you're interesting only in efficient loading of the initial sorted data? 我假设你只对有效加载初始排序数据感兴趣?

The standard map :: map (InputIterator first, InputIterator last) constructor seems to do the right thing. 标准的map :: map(InputIterator first,InputIterator last)构造函数似乎做对了。

"For the iterator constructor, linear in the distance between the iterators (copy constructions) if the elements are already sorted according to comp. For unsorted sequences, linearithmic (N*logN) in that distance (sorting,copy constructions)." “对于迭代器构造函数,如果元素已根据comp排序,则迭代器之间的距离是线性的(复制结构)。对于未排序的序列,该距离中的线性(N * logN)(排序,复制结构)。”

http://www.sgi.com/tech/stl/Map.html http://www.sgi.com/tech/stl/Map.html

I think map(InputIterator f, InputIterator l) might help you, but I don't know if it takes into account the data being sorted :/ 我认为map(InputIterator f,InputIterator l)可能对你有帮助,但我不知道它是否考虑了被排序的数据:/

I don't believe you have any guarantees as to the actual data structure used under the hood for an STL map. 我不相信你对STL地图引擎盖下使用的实际数据结构有任何保证。 Additionally, consider that the order you insert the data (you state it is sorted) could have a negative impact on performance if the map did not perform rotations! 另外,如果地图没有执行旋转,请考虑插入数据的顺序(您声明它已排序)可能会对性能产生负面影响! Of course rotations implies that a self-balancing tree is used rather than a skip-list, splay tree, or any other data structure decided upon by the library author. 当然,旋转意味着使用自平衡树而不是跳过列表,展开树或库作者决定的任何其他数据结构。

Likely the time spent retrieving the data from the database will dwarf the time spent adding the sorted data into the map. 从数据库中检索数据所花费的时间可能会使将排序数据添加到地图中所花费的时间相形见绌。 A possible optimization would be to NOT retrieve the data in sorted order. 可能的优化是不按排序顺序检索数据。 The map will not care about the sorting. 地图不关心排序。

The std::map::insert(iterator, pair) function has amortized constant cost if the input is sorted. 如果输入已排序,则std::map::insert(iterator, pair)函数已分摊常量成本。 Reading in the entire data set means you get O(N). 读取整个数据集意味着你得到O(N)。 (Note that this program has correct semantics regardless of whether the input is sorted.) (请注意,无论输入是否已排序,此程序都具有正确的语义。)

#include <map>
#include <iostream>
int main() {
 std::map<int, int> m;
 int a, b;
 for(std::map<int,int>::iterator it = m.begin();
     std::cin >> a >> b;
     it = m.insert(it, std::pair<int,int>(a,b))) {
  /* nothing */
 }
}

If you must use a map, then consider using the gcc policy based containers if you can. 如果必须使用地图,请考虑使用基于gcc策略的容器(如果可以)。 They are much faster than the STL. 它们比STL快得多。

http://gcc.gnu.org/onlinedocs/libstdc++/ext/pb_ds/interface.html http://gcc.gnu.org/onlinedocs/libstdc++/ext/pb_ds/interface.html

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

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