help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Amazing Performance Difference between vec[] and iterators


From: Mark
Subject: Re: Amazing Performance Difference between vec[] and iterators
Date: Wed, 19 Jul 2006 12:36:13 -0400
User-agent: Mozilla/5.0 (X11; U; SunOS sun4u; en-US; rv:1.7.12) Gecko/20050928

thoth39 wrote:
Ulrich Lauther wrote:

Paulo Matos <pocmatos@gmail.com> 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)

Try

unsigned int size = v.size();
for (unsigned int k = 0; k < size; ++k)

in your version size() is evaluated size times


Likewise, instead of

for(vector<int>::const_iterator it = v.begin(); it != v.end(); ++it)

try

for(vector<int>::const_iterator it = v.begin(), end = v.end(); it !=
end; ++it)

to avoid calling v.end() every time.


Aren't compilers smart enough yet to in effect do that if it sees there's
nothing done inside the loop that would change the vector's size?
That seems like something an optimizer should do.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]