const_iterators更快吗? [英] Are const_iterators faster?
问题描述
我们的编码指南喜欢 const_iterator
,因为它们比正常的迭代器
快一点。在使用 const_iterator
时,编译器似乎优化了代码。
Our coding guidelines prefer const_iterator
, because they are a little faster compared to a normal iterator
. It seems like the compiler optimizes the code when you use const_iterator
.
这是真的吗?如果是的话,在内部真正发生了什么使 const_iterator
更快?
Is this really correct? If yes, what really happens internally that makes const_iterator
faster?.
编辑:我写了小测试检查 const_iterator
vs 迭代器
并找到不同的结果:
I wrote small test to check const_iterator
vs iterator
and found varying results:
10,000个对象 const_terator
花费了几毫秒(大约16毫秒)。但不总是。
For iterating 10,000 objects const_terator
was taking a few milliseconds (around 16 ms) less. But not always. There were iterations in which both were equal.
推荐答案
如果没有别的,一个 const_iterator
更好,因为它告诉任何人阅读的代码我只是迭代这个容器,不搞乱对象包含。
If nothing else, a const_iterator
reads better, since it tells anyone reading the code "I'm just iterating over this container, not messing with the objects contained".
这是一个伟大的胜利,不介意任何表现差异。
That's a great big win, never mind any performance differences.
这篇关于const_iterators更快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!