Boost logo

Boost :

Subject: Re: [boost] BOOST_FOREACH slow?
From: Maik Beckmann (beckmann.maik_at_[hidden])
Date: 2008-11-18 03:21:40


Am Dienstag, 18. November 2008 schrieb Peter Bartlett:
> Quoting Hansi <hansipet_at_[hidden]>:
> > regarding to the benchmark link posted from Michael Marcin
> > BOOST_FOREACH is 3 times slower then other equivalents...it 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. 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 . . .

To underline these results, here what gcc-4.3.2 gives on x86_64 linux:
{{{
maik_at_harald:~$ g++ -O0 test.cpp -I /home/maik/workspace/boost
maik_at_harald:~$ ./a.out
Iterator accumulate took 1.39 seconds.
Pointer accumulate took 0.34 seconds.
Iterator for loop took 1.4 seconds.
Pointer for loop took 0.34 seconds.
Index for loop took 0.59 seconds.
Index for_each took 1.73 seconds.
Pointer for_each took 0.63 seconds.
BOOST_FOREACH took 4.59 seconds.

maik_at_harald:~$ g++ -O2 test.cpp -I /home/maik/workspace/boost
maik_at_harald:~$ ./a.out
Iterator accumulate took 0.1 seconds.
Pointer accumulate took 0.09 seconds.
Iterator for loop took 0.1 seconds.
Pointer for loop took 0.09 seconds.
Index for loop took 0.1 seconds.
Index for_each took 0.09 seconds.
Pointer for_each took 0.1 seconds.
BOOST_FOREACH took 0.1 seconds

maik_at_harald:~$ g++ -DNDEBUG -O2 test.cpp -I /home/maik/workspace/boost
maik_at_harald:~$ ./a.out
Iterator accumulate took 0.09 seconds.
Pointer accumulate took 0.1 seconds.
Iterator for loop took 0.09 seconds.
Pointer for loop took 0.1 seconds.
Index for loop took 0.09 seconds.
Index for_each took 0.1 seconds.
Pointer for_each took 0.09 seconds.
BOOST_FOREACH took 0.1 seconds.

maik_at_harald:~$ g++ -DNDEBUG -O3 test.cpp -I /home/maik/workspace/boost
maik_at_harald:~$ ./a.out
Iterator accumulate took 0.09 seconds.
Pointer accumulate took 0.09 seconds.
Iterator for loop took 0.08 seconds.
Pointer for loop took 0.09 seconds.
Index for loop took 0.09 seconds.
Index for_each took 0.09 seconds.
Pointer for_each took 0.09 seconds.
BOOST_FOREACH took 0.09 seconds.
}}}
where /home/maik/workspace/boost is a working copy of trunk.

-- Maik


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk