简体   繁体   中英

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)

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

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; see the following link: Benchmarks of STL containers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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