From: Seungbeom Kim on
pedro wrote:
>
> I just tried the suggestion from JC and I got a vast improvement from
> the iterator version:
>
> 499999500000000
> 499999500000
> 499999500000000
>
> 3.543
> 0.002
> 1.58
>
> This is my iterator version:
>
> sum = 0;
> vector<int>::iterator j = num.begin();
> for(int i = 0; i < iter; ++i)
> for(; j != num.end(); ++j)
> sum += *j;
>

Don't you have to initialize j in each iteration?

--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Chris Uzdavinis on
On Jan 3, 10:59 am, pedro <pedroke...(a)gmail.com> wrote:
> I just tried the suggestion from JC and I got a vast improvement from
> the iterator version:
>
> 499999500000000
> 499999500000
> 499999500000000
>
> 3.543
> 0.002
> 1.58

Something seems fishy about a result being "that" much better. Try
switching the order of your tests, so that the for loop is done in the
middle and the iterators are done first. Or do each test twice
(individually timed.) Might be interesting.

Chris


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Matthias Kluwe on
Hi!

On 1 Jan., 16:54, brang...(a)cix.co.uk (Dave Harris) wrote:
> pal_...(a)yahoo.co.in (Arijit) wrote (abridged):
>
> > As expected, accumulate is the fastest of them all, but only by a
> > narrow margin. The shocker is using iterators is nearly 5 times
> > slower than indexing, whereas I expected the two to be nearly at
> > par. I don't understand why this should happen.
>
> Perhaps you have VC++'s checked iterators enabled. They are enabled by
> default even in release builds. Try adding:
> #define _SECURE_SCL=0

Annoying. Please forgive the platform specific question:

Does anynone know of any other "assistance" MS has thought of that I'd
have to #define away if I do not want to pay for them?

Regards,
Matthias


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: ThosRTanner on
On Jan 3, 4:59 pm, pedro <pedroke...(a)gmail.com> wrote:

>
> This is my iterator version:
>
> sum = 0;
> vector<int>::iterator j = num.begin();
> for(int i = 0; i < iter; ++i)
> for(; j != num.end(); ++j)
> sum += *j;

Thats because your 2nd loop now only does stuff the first time. It'd
be (slightly) more productive to leave the declaration of j in the for
loop and stash the value of num.end()


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]