Boost logo

Boost :

Subject: Re: [boost] BOOST_FOREACH slow?
From: Bjørn Roald (bjorn_at_[hidden])
Date: 2008-11-19 01:07:25


David Abrahams wrote:
> Just curious: you didn't compare with std::for_each (e.g. to see the
> effects of loop unrolling), did you?
>

For whatever it is worth I added:

 {
    boost::timer timer;
    sum_func sum = std::for_each( data.begin(), data.end(), sum_func() );
    double elapsedtime = timer.elapsed();
    std::cout << "std::for_each took "
          << elapsedtime << " seconds."
          << std::endl;
    force_calculation(sum.sum);
  }

to the benchmark and ran it on my ubuntu box,
Linux 2.6.27-7-generic #1 SMP Tue Nov 4 19:33:06 UTC 2008 x86_64 GNU/Linux
g++ (Ubuntu 4.3.2-1ubuntu11) 4.3.2

I got:
g++ -I../../boost_1_37_0 foreach_benchmark.cpp -o foreach_benchmark
Iterator accumulate took 1.4 seconds.
Pointer accumulate took 0.34 seconds.
Iterator for loop took 1.39 seconds.
Pointer for loop took 0.34 seconds.
Index for loop took 0.6 seconds.
Index for_each took 1.73 seconds.
Pointer for_each took 0.63 seconds.
BOOST_FOREACH took 4.36 seconds.
std::for_each took 1.73 seconds.

g++ -O3 -I../../boost_1_37_0 foreach_benchmark.cpp -o foreach_benchmark
Iterator accumulate took 0.09 seconds.
Pointer accumulate took 0.09 seconds.
Iterator for loop took 0.09 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.
std::for_each took 0.09 seconds.

-- 
Bjørn

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