Boost logo

Boost :

Subject: Re: [boost] [local] Help for the Alternatives section
From: lcaminiti (lorcaminiti_at_[hidden])
Date: 2011-03-29 12:37:29


lcaminiti wrote:
>
> benchmark numbers. I will run correct tests more along the lines of what
> you suggested.
>

Hello all,

I am looking into why Boost.Local has a long run-time and I run into
something strange that I cannot explain... I am still looking into this but
I wanted to ask if anyone has some insight.

Consider the following code which uses Boost.ScopeExit type deduction and a
local functor class (these are bits that Boost.Local uses internally):

#include <boost/local/function.hpp>
#include
#include
#include

int main() {
    double sum = 0.0;
    int factor = 10;

    typedef void (*tag)(int factor);
    typedef BOOST_TYPEOF(boost::scope_exit::aux::wrap(
            boost::scope_exit::aux::deref(factor, (tag)0))) wrapped_t;
    typedef wrapped_t::type capture_t;
    struct params_t {
        typedef capture_t param_t;
        boost::scope_exit::aux::member<param_t, tag> param;
    } params = {
          { boost::scope_exit::aux::deref(factor, (tag)0) }
    };

    // *******
    std::cout << params.param.value << std::endl; // (1)
    std::vector v(1000000); // (2)
    // *******

    class local_add {
    public:
        explicit local_add(double& _sum, int _factor):
                sum_(_sum), factor_(_factor) {
        }
        void operator()(double num) {
            return body(factor_, sum_, num);
        }
    private:
        double& sum_;
        int factor_;
        
        void body(const int factor, double& sum, double num)
                const {
            sum += factor * num;
        }
    } add(sum, factor);

    std::fill(v.begin(), v.end(), 10);
    for (size_t i = 0; i < 10000; ++i) {
        for (size_t j = 0; j < v.size(); ++j) {
            add(v[j]);
        }
    }

    std::cout << sum << std::endl;
    return 0;
}

This compiles and runs as:

$ time g++ -O3 -Wall -Wextra -I../../.. l09.cpp -ol09

real 0m5.969s
user 0m0.994s
sys 0m1.435s

$ time ./l09
10
1e+12

real 0m16.297s
user 0m15.827s
sys 0m0.109s

But if I simply swap lines (1) and (2):

$ diff -u l09.cpp l08.cpp
--- l09.cpp 2011-03-29 12:21:16.034974600 -0400
+++ l08.cpp 2011-03-29 12:21:08.160478600 -0400
@@ -20,8 +20,8 @@
     };
 
 
- std::cout << params.param.value << std::endl;
     std::vector v(1000000);
+ std::cout << params.param.value << std::endl;
 
 
     class local_add {

Then it runs _much_ slower:

$ time g++ -O3 -Wall -Wextra -I../../.. l08.cpp -ol08

real 0m5.875s
user 0m0.821s
sys 0m1.231s

$ time ./l08
10
1e+12

real 0m44.469s
user 0m43.874s
sys 0m0.015s

Why swapping lines (1) and (2) changes the run-time? I don't understand it
at all... I must be missing something...

Thank you in advance.

--
Lorenzo
http://boost.2283326.n4.nabble.com/file/n3415414/l08.exe l08.exe 
http://boost.2283326.n4.nabble.com/file/n3415414/l09.exe l09.exe 
--
View this message in context: http://boost.2283326.n4.nabble.com/local-Help-for-the-Alternatives-section-tp3408469p3415414.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

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