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: thoth39
Subject: Re: Amazing Performance Difference between vec[] and iterators
Date: 18 Jul 2006 08:57:48 -0700
User-agent: G2/0.2

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.



reply via email to

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