简体   繁体   English

比较两个C ++迭代器的性能成本

[英]Performance cost of comparing two C++ iterators

In general, what is the performance cost of an equality comparison between two STL container iterators? 通常,两个STL容器迭代器之间的相等比较的性能成本是多少? I'm only talking about defined operations; 我只谈论定义的操作; that is, comparing two iterators referring to the same object. 也就是说,比较两个引用同一对象的迭代器。

My specific use-case is that I have a std::map that could potentially have very large elements, lots of elements, or both. 我的具体用例是我有一个std::map ,可能有非常大的元素,很多元素,或两者兼而有之。 If an equality comparison between two iterators over such a map have hidden penalties that I'm not aware of, it could impact the performance of my code. 如果在这样的映射上两个迭代器之间的相等比较具有我不知道的隐藏惩罚,那么它可能会影响我的代码的性能。

Generally, the performance of comparing two iterators depends on the implementation of the STL. 通常,比较两个迭代器的性能取决于STL的实现。

However, concerning the time complexity , the C++ standard imposes the restriction that comparison of input iterators (and thus forward iterators, bidirectional iterators and random access iterators) takes amortized constant time. 但是,关于时间复杂度 ,C ++标准强加了输入迭代器(以及前向迭代器,双向迭代器和随机访问迭代器)的比较采用分摊的常量时间的限制。 Particularly, this means for an std::map<std::string, int> , that its iterators cannot be compared by comparing the keys for equality, because that would be linear with respect to the length of the key. 特别是,对于std::map<std::string, int> ,这意味着无法通过比较相等的键来比较其迭代器,因为这将与键的长度成线性关系。

Most of STL containers operator==() is just raw pointer comparison. 大多数STL容器operator==()只是原始指针比较。 Which is meaningless unless it's for boundaries checking. 除非用于边界检查,否则这是没有意义的。 More over, if you are comparing iterators from different containers - it's undefined behaviour. 更重要的是,如果你要比较来自不同容器的迭代器 - 它是未定义的行为。

If you override this operator or use external comparison function, perfomance depends on how large are object you are comparing. 如果覆盖此运算符或使用外部比较函数,则性能取决于您要比较的对象的大小。

Probably I got your question wrong, it's not 100% clear what do you mean by "iterator comparison" and what's your use case. 可能我的问题是错的,并不是100%清楚“迭代器比较”是什么意思,你的用例是什么。

The draft Standard states that iterator operations are amortized constant time 标准草案规定迭代器操作按常数时间摊销

24.2.1 In general [iterator.requirements.general] 24.2.1一般[iterator.requirements.general]

8 All the categories of iterators require only those functions that are realizable for a given category in constant time (amortized). 8所有迭代器类别只需要在常量时间内(摊销)可以为给定类别实现的那些函数。 Therefore, requirement tables for the iterators do not have a complexity column. 因此,迭代器的需求表没有复杂性列。

If you look at the signatures of iterator operations, there are no parameters or return types that correspond to the underlying elements T themselves, only T* and T& are required. 如果查看迭代器操作的签名,则没有与底层元素T本身对应的参数或返回类型,只需要T*T& Even operator== does not have to directly compare two arbitrarily large elements T themselves. 甚至operator==也不必直接比较两个任意大的元素T本身。

However, this does not give a hard real-time upper-bound for iterator operations. 但是,这并没有为迭代器操作提供硬实时上限。 In particular, iterators can do very costly bounds checking , but these Debug mode security guards can usually be left out in Release builds. 特别是,迭代器可以执行非常昂贵的边界检查 ,但这些调试模式安全保护通常可以在发布版本中省略。

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

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