performance - call to condition on for loop (c++) -


here simple question have been wondering long time : when loop such 1 :

for (int = 0; < myvector.size() ; ++i) {     // loop } 

as condition i < myvector.size() checked each time, should store size of array inside variable before loop prevent call size() each iteration ? or compiler smart enough ?

mysize = myvector.size();    (int = 0; < mysize ; ++i) {     // loop } 

and extend question more complex condition such i < myvector.front()/myvector.size()

edit : don't use myvector inside loop, juste here give ending condition. , more complex condition ?

the answer depends on contents of loop–it may modify vector during processing, modifying size.

however if vector scanned can safely store size in advance:

for (int = 0, mysize = myvector.size(); < mysize ; ++i) {     // loop } 

although in classes functions 'get current size' inline getters:

class xxx { public:     int size() const { return msize; }     .... private:     int msize;     .... }; 

so compiler can reduce call reading int variable, consequently prefetching length gives no gain.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -