Boost logo

Boost :

Subject: Re: [boost] BOOST_FOREACH slow?
From: Eric Niebler (eric_at_[hidden])
Date: 2008-11-18 12:18:51

Peter Bartlett wrote:
> Quoting Hansi <hansipet_at_[hidden]>:
>> regarding to the benchmark link posted from Michael Marcin
>> BOOST_FOREACH is 3 times slower then other is a lot...
>> Best regards
>> Hansjörg
> And that should make you sceptical of the results! Once you strip away
> the TMP and macros, FOREACH is not much different from a for lopp. There
> is an additional boolean [or so?] that looks ripe for optimization.

Yes, one extra boolean test/set per iteration. That's all you should pay
for using BOOST_FOREACH when optimizations are turned on. I can't get
rid of it without also getting rid of the ability to iterate over a
sequence by reference.

> Checking that premise: I just re-ran Michael's test with VC9 on full
> optimization and _SECURE_SCL=0. My results are:
> Iterator accumulate took 0.235 seconds.
> Pointer accumulate took 0.265 seconds.
> Iterator for loop took 0.234 seconds.
> Pointer for loop took 0.25 seconds.
> Index for loop took 0.281 seconds.
> Index for_each took 0.235 seconds.
> Pointer for_each took 0.25 seconds.
> BOOST_FOREACH took 0.25 seconds.
> MSVC for each took 0.234 seconds.
> Press any key to continue . . .

Thanks Peter!

Eric Niebler
BoostPro Computing

Boost list run by bdawes at, gregod at, cpdaniel at, john at