[英]Does std::map::find performance depend on the key size?
Say I have the the following map
definition: 说我有以下
map
定义:
std::map<string,Storage>
where the key is the string representation of a Storage
class instance. 其中键是
Storage
类实例的字符串表示形式。
My question is, even though it stated that map::find complexity is logarithmic in size , does the string
size has any influence on the performance? 我的问题是,即使它声明map :: find 复杂度的大小是对数的 ,
string
大小是否对性能有任何影响?
The reason I have this map
is to enable fast access to Storage
class instance. 我有这个
map
的原因是为了能够快速访问Storage
类实例。 However, what if the string representation of Storage
classes is very long? 但是,如果
Storage
类的字符串表示很长,该怎么办? Is there a max string size that if exceeded makes the use of map
redundant? 是否存在最大字符串大小(如果超出)会使
map
冗余使用?
My intuition tells me that if the string representation of Storage
classes is very long then comparing the classes themselves using operator==
would be expensive as well. 我的直觉告诉我,如果
Storage
类的字符串表示很长,那么使用operator==
比较类本身也会很昂贵。 So no matter how long the string is, I'm better of using map
所以无论字符串有多长,我都更善于使用
map
Yes, the map has to perform a less-than comparison of the keys. 是的,地图必须执行小于比较的键。 This is a lexicographical comparison and is linear WRT the string size.
这是一个字典比较,是字符串大小的线性WRT。
This does not affect the time complexity of the find
method, which refers to the number of comparisons required. 这不会影响
find
方法的时间复杂度, find
方法指的是所需的比较次数。 It affects the constant factor. 它会影响常数因素。
Whether that matters or not in your application should be determined empirically. 在您的申请中是否重要应该根据经验确定。
std::map
uses lexicographical ordering for the key type. std::map
使用密钥类型的词典排序。 This means that performance of search operations on the map is determined by the shared prefix of keys in the map and the key you are looking for. 这意味着地图上搜索操作的性能取决于地图中键的共享前缀和您要查找的键。 If you have many keys sharing a very long prefix, and you search for a key with that prefix, performance will dwindle.
如果您有许多密钥共享一个非常长的前缀,并且您搜索具有该前缀的密钥,性能将会下降。
For example, this is expensive: 例如,这很昂贵:
aaaaaa <millions of a's> aaaa
aaaaaa <millions of a's> aaab
aaaaaa <millions of a's> aaac
This is cheap: 这很便宜:
aaaaaa <millions of a's> aaaa
baaaaa <millions of a's> aaaa
caaaaa <millions of a's> aaaa
The "complexity" of a map lookup is defined in units of comparisons. 地图查找的“复杂性”以比较为单位定义。 So "logarithmic in size" means it will perform
O(log(size()))
key comparisons. 所以“对数大小”意味着它将执行
O(log(size()))
键比较。 For expensive key comparisons, this indeed affects actual performance. 对于昂贵的密钥比较,这确实会影响实际性能。
Yes, comparing two strings(with a long shared prefix) are generally O(n) complexity. 是的,比较两个字符串(具有长共享前缀)通常是O(n)复杂度。
if strings do not share a long prefix, it may take less time. 如果字符串不共享长前缀,则可能需要更少的时间。
Generally, longer strings take longer time to compare. 通常,较长的字符串需要较长时间进行比较。
Maybe you should consider unordered_map (hash_table) if key is a string. 如果key是一个字符串,也许你应该考虑unordered_map(hash_table)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.