Paulo Matos wrote: > Hi all, > > I've wanted to know if there was any performance difference between > using: > for(vector<int>::const_iterator it = v.begin(); it != v.end(); ++it) > > and > > for(unsigned int k = 0; k < v.size(); ++k) > > and it seems there is with gcc 4.1.1, using -O3, the later is almost > two times slower. > The program is at: > http://sat.inesc-id.pt/~pocm/archive/vec.cc > Then do: > $ g++ -O3 vec.cc -o vec-iter -DITER > $ g++ -O3 vec.cc -o vec > > I got these times: > $ time ./vec > 5030 elements in array. > sum: 27665 > > real 0m7.123s > user 0m7.108s > sys 0m0.008s > > $ time ./vec-iter > 5030 elements in array. > sum: 27665 > > real 0m4.699s > user 0m4.672s > sys 0m0.020s > > Any comments on these results? They don't seem intuitive to me. Why the > difference?
just guessing.. could that be the overhead of calling size() and operator[] each time? try to store the size() to a local variable before the for loop.. also see the implementation of size() and operator[] in bits/stl_vector.h > > Cheers, > > Paulo Matos _______________________________________________ help-gplusplus mailing list help-gplusplus@gnu.org http://lists.gnu.org/mailman/listinfo/help-gplusplus