Boost logo

Boost Users :

Subject: Re: [Boost-users] Why use 'functional composition' with boost::bind
From: Christopher Jefferson (chris_at_[hidden])
Date: 2010-08-05 06:40:42

On 3 Aug 2010, at 15:35, Wolfram Brenig wrote:

> Hi,
> as a casual/newbie boost-user I'm trying to come to
> grips with why/when nested functional composition with
> boost::bind might be of help - following Karlsson's
> book, in its fifth printing.

You are not comparing like with like in your example, the compiler is over optimising.

I tried comparing these functions:

void f1(vector<int>& v)
     for(int i=0;i<v.size();i++) {
        v[i] = (random()*M_PI)/RAND_MAX;
        double x = sin(v[i]); x = x*x;
        double y = cos(v[i]); y = y*y;
        v[i] = x + y;

void f2(vector<int>& v)
     for(int i=0;i<v.size();i++) {
        v[i] = (random()*M_PI)/RAND_MAX;
     for(int i=0;i<v.size();i++) {
        v[i] = sin(v[i])*sin(v[i])+cos(v[i])*cos(v[i]);

void f3(vector<int>& v)


In a seperate file, I compiled:

#include <vector>
using namespace std;

void f1(vector<int>& v);
void f2(vector<int>& v);
void f3(vector<int>& v);

int main(void)
        std::vector<int> v(10000);
        for(int i = 0; i < 10000; ++i)
Replacing FUN with f1,f2 and f3.

Using g++ 4.4 and -O3 there was no difference in performance between them. In eariler versions of g++, the first one was faster, as the compiler didn't figure out it could fuse the loops together.

One very useful g++ flag is "-fdump-tree-all". This will dump out a number of files which show you how g++ is optimising the code. The format isn't documented, but it is fairly readable. Using this flag, you will see that f1,f2 and f3 are all turned into the same code in a recent g++.


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at