What is the performance difference between using an iterator to loop through an STL map, versus a vector? I'd like to use the map key for insertion, deletion, and some accesses, but I also need to do regular accesses to every element in the map.
使用迭代器循环STL映射与向量之间的性能差异是什么?我想使用map键进行插入,删除和一些访问,但我还需要定期访问map中的每个元素。
6 个解决方案
#1
With both map and vector, iterating through the entire collection is O(N). however (like list vs vector) vector stores elements contiguously, so accessing the next element is much cheaper because it will use cache optimally, whereas the map won't.
使用map和vector,遍历整个集合的是O(N)。然而(像列表与矢量一样)矢量连续存储元素,因此访问下一个元素要便宜得多,因为它将最佳地使用缓存,而地图则不会。
But since you need to have lookup based on keys, there isn't really an alternative. You could use a vector of pairs sorted on the first element, but if the collection needs to be mutable this is going to be very slow. Just use a map.
但是,由于您需要基于密钥进行查找,因此没有其他选择。你可以使用在第一个元素上排序的对矢量,但如果集合需要是可变的,那么这将是非常慢的。只需使用地图。
#3
This link has a nice table of performance for various operations on all of the STL containers.
此链接有一个很好的性能表,可用于所有STL容器上的各种操作。
Generally speaking, if you need to do a lot of inserting, removing or searching based on a key then the map is the way to go.
一般来说,如果您需要根据密钥进行大量插入,删除或搜索,那么地图就是您的选择。
If you only need to set up the container once and then access it like an array then use a vector.
如果您只需要设置容器一次然后像数组一样访问它,那么使用向量。
EDIT: Performance table of STL container operations:
编辑:STL容器操作的性能表:
#4
Iterating through a map may be linear but practically , it is not so efficient from the implementations in C++ . So my advice is to use a vector and use another map to locate the items in the vector in linear time .
迭代映射可能是线性的,但实际上,它在C ++中的实现并不那么有效。所以我的建议是使用矢量并使用另一个地图以线性时间定位矢量中的项目。
#5
Browsing the tree is not expensive (grosso modo like following a linked list), you won't benefit from the cache as much with a vector, but generally it's what you do when you iterate that is expensive, not the iteration itself.
浏览树并不昂贵(grosso modo就像跟踪链表一样),你不会像使用向量那样从缓存中受益,但通常你在迭代时所做的事情是昂贵的,而不是迭代本身。
Could you tell us more about what you expect to do when you iterate through the whole map?
你可以告诉我们更多关于你在迭代整个地图时的期望吗?
#6
Use map if you need fast way of access by the key. Otherwise use vector all the time unless some performance issues will be discovered with profiler.
如果您需要通过密钥快速访问方式,请使用地图。否则,请始终使用向量,除非使用分析器发现某些性能问题。
#1
With both map and vector, iterating through the entire collection is O(N). however (like list vs vector) vector stores elements contiguously, so accessing the next element is much cheaper because it will use cache optimally, whereas the map won't.
使用map和vector,遍历整个集合的是O(N)。然而(像列表与矢量一样)矢量连续存储元素,因此访问下一个元素要便宜得多,因为它将最佳地使用缓存,而地图则不会。
But since you need to have lookup based on keys, there isn't really an alternative. You could use a vector of pairs sorted on the first element, but if the collection needs to be mutable this is going to be very slow. Just use a map.
但是,由于您需要基于密钥进行查找,因此没有其他选择。你可以使用在第一个元素上排序的对矢量,但如果集合需要是可变的,那么这将是非常慢的。只需使用地图。
#2
Iterating through every element of a map takes O(n) time. wikipedia
迭代地图的每个元素需要O(n)时间。*
#3
This link has a nice table of performance for various operations on all of the STL containers.
此链接有一个很好的性能表,可用于所有STL容器上的各种操作。
Generally speaking, if you need to do a lot of inserting, removing or searching based on a key then the map is the way to go.
一般来说,如果您需要根据密钥进行大量插入,删除或搜索,那么地图就是您的选择。
If you only need to set up the container once and then access it like an array then use a vector.
如果您只需要设置容器一次然后像数组一样访问它,那么使用向量。
EDIT: Performance table of STL container operations:
编辑:STL容器操作的性能表:
#4
Iterating through a map may be linear but practically , it is not so efficient from the implementations in C++ . So my advice is to use a vector and use another map to locate the items in the vector in linear time .
迭代映射可能是线性的,但实际上,它在C ++中的实现并不那么有效。所以我的建议是使用矢量并使用另一个地图以线性时间定位矢量中的项目。
#5
Browsing the tree is not expensive (grosso modo like following a linked list), you won't benefit from the cache as much with a vector, but generally it's what you do when you iterate that is expensive, not the iteration itself.
浏览树并不昂贵(grosso modo就像跟踪链表一样),你不会像使用向量那样从缓存中受益,但通常你在迭代时所做的事情是昂贵的,而不是迭代本身。
Could you tell us more about what you expect to do when you iterate through the whole map?
你可以告诉我们更多关于你在迭代整个地图时的期望吗?
#6
Use map if you need fast way of access by the key. Otherwise use vector all the time unless some performance issues will be discovered with profiler.
如果您需要通过密钥快速访问方式,请使用地图。否则,请始终使用向量,除非使用分析器发现某些性能问题。