简体   繁体   English

通过索引访问向量的速度:向后与向前

[英]Speed of accessing a vector by index: backwards vs forward

Due to caching effects the forward iteration of a vector will be faster than (eg) random access: 由于缓存效应,向量的前向迭代将比(例如)随机访问更快:

for (unsigned i=0; i<vec.size(); i++) {
    vec[i] = something(i); 
}

Now I need to iterate it backwards: 现在,我需要向后迭代:

for (unsigned i=vec.size(); i-->0; ) {
    vec[i] = something(i);
}

On my system there seems to be no speed difference. 在我的系统上,似乎没有速度差异。 Can I assume that the same caching effects apply here and therefore the loops will have the same speed on most systems? 我是否可以假定在这里应用相同的缓存效果,因此在大多数系统上循环的速度相同?

Making assumptions about hardware is almost always a poor choice - unless you have a specific subset of hardware in mind. 关于硬件的假设几乎总是一个糟糕的选择-除非您牢记特定的硬件子集。 If you intend your code to run near-exclusively on modern x86 hardware (windows 8 desktop machines for example) then it is fair to assume that this reverse iteration will be equivalent in speed (as reverse_iterators generally are) 如果您打算让代码几乎完全在现代x86硬件(例如Windows 8台式机)上运行,则可以假设此反向迭代的速度相同(通常与reverse_iterators相同 )。

However, if your aiming for mobile technologies, embedded systems, or older systems there is a much greater degree of variance between what can be expected. 但是,如果您针对移动技术,嵌入式系统或较旧的系统,则预期之间会有很大程度的差异。

Personally, I've spend a good four years developing optimised software commercially for desktop machines - In that time I have never needed to optimise a vector iteration . 就个人而言,我已经花了四年的时间为台式机商业开发优化软件-那时我从来不需要优化向量迭代

If you find yourself profiling your software on a platform and find that there is a need for optimisation in this regard - you may be better allocating an array and manually iterating over the elements . 如果您发现自己在平台上对软件进行了性能分析,并发现在这方面需要优化, 则最好分配一个数组并手动迭代这些元素 However more often than not, such optimisations are overzealous early in your development. 但是,在您的开发初期,这样的优化往往过于热情。

You could, I have no doubt, create your own iterator class, vector, or array class to provide faster iterations in your specific case. 毫无疑问,您可以创建自己的迭代器类,向量或数组类,以在您的特定情况下提供更快的迭代。 But doing so will complicate your code greatly - something that will eventually cause issues when it comes to debugging your code. 但是这样做会极大地使您的代码复杂化-最终会在调试代码时引起问题。

“Debugging is twice as hard as writing the code in the first place. “调试的难度是一开始编写代码的两倍。 Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” ~ Brian Kernighan 因此,如果您尽可能聪明地编写代码,那么就定义而言,您就不足以调试它。”〜Brian Kernighan

Personally I would recommend Unless you have an example of a targeted platform-set you intend on deploying into that is abnormal, or have a specific scenario where you can produce evidence of a speed difference. 我个人建议,除非您有一个目标平台集的例子,否则您打算将其部署到异常的环境中,或者有一个特定的场景可以产生速度差异的证据。 Assume the Standard Library Vector is appropriately fast. 假设标准库向量适当地快。

That said, depending on the workload you wish to preform, you may find a std::list more appropriate if you have very large amounts of data; 也就是说,根据您希望执行的工作量,如果您有大量数据,则可能会发现更合适的std :: list; see the following link: Benchmarks of STL containers 请参阅以下链接: STL容器基准

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

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