Llvm g ++ and boost function
I'm trying to undo if the overhead introduced by boost :: function for evaluating math functions is invaluable compared to using it function templates
.
The code for the test I'm using is below.
With traditional g++
overheads, they are boost::function
invaluable:
$ g++ -O3 main.cxx
$ ./a.out
METHOD INTEGRAL TIME TO COMPUTE (SEC)
Direct 0.379885 3.360000
Function template 0.379885 3.380000
Boost function 0.379885 3.400000
With llvm-g++
there is a gain of 1.5 for templates function
, but no gain for boost::function
.
$ llvm-g++ -O3 main.cxx
METHOD INTEGRAL TIME TO COMPUTE (SEC)
Direct 0.379885 2.170000
Function template 0.379885 2.160000
Boost function 0.379885 3.360000
Is it possible to get a gain of 1.5 for boost::function
and llvm-g++
?
#include <boost/function.hpp>
#include <math.h>
#include <stdio.h>
typedef unsigned int UInt;
using namespace std;
//=============================================================================
// chrono
//=============================================================================
class Chrono
{
clock_t t1_,t2_,dt_;
public:
Chrono(){}
void start() { t1_=clock(); };
void stop() { t2_=clock(); };
double diff() { return ( (double)( t2_ - t1_) ) / CLOCKS_PER_SEC; };
};
//=============================================================================
// function to integrate
//=============================================================================
inline double fct(double x)
{
return 1. / (1.+exp(x));
}
//=============================================================================
// using direct method
//=============================================================================
double direct(double a, double b, UInt numSamplePoints)
{
double delta = (b-a) / (numSamplePoints-1);
double sum = 0.;
for (UInt i=0; i < numSamplePoints-1; ++i)
sum += 1. / (1. + exp(a + i*delta));
return sum * delta;
}
//=============================================================================
// using function template
//=============================================================================
template<double functionToIntegrate(double)>
double integrate(double a, double b, UInt numSamplePoints)
{
double delta = (b-a) / (numSamplePoints-1);
double sum = 0.;
for (UInt i=0; i < numSamplePoints-1; ++i)
sum += functionToIntegrate(a + i*delta);
return sum * delta;
}
//=============================================================================
// using Boost function
//=============================================================================
typedef boost::function<double ( double )> fct_type;
class IntegratorBoost {
public:
fct_type functionToIntegrate;
IntegratorBoost(fct_type fct): functionToIntegrate(fct){}
double integrate(double a, double b, UInt numSamplePoints)
{
double delta = (b-a) / (numSamplePoints-1);
double sum = 0.;
for (UInt i=0; i < numSamplePoints-1; ++i)
sum += functionToIntegrate(a + i*delta);
return sum * (b-a) / numSamplePoints;
}
};
//=============================================================================
// main
//=============================================================================
int main()
{
double integral;
UInt numSamplePoints = 5E07;
Chrono chrono;
printf("%-20s%-10s%-30s\n","METHOD","INTEGRAL","TIME TO COMPUTE (SEC)");
// Direct
chrono.start();
integral = direct(0., 1., numSamplePoints);
chrono.stop();
printf("%-20s%-10f%-30f\n","Direct",integral,chrono.diff());
// Function template
chrono.start();
integral = integrate<fct>(0., 1.,numSamplePoints);
chrono.stop();
printf("%-20s%-10f%-30f\n","Function template",integral,chrono.diff());
// Boost function
chrono.start();
IntegratorBoost intboost(fct);
integral = intboost.integrate(0.,1.,numSamplePoints);
chrono.stop();
printf("%-20s%-10f%-30f\n","Boost function",integral,chrono.diff());
}
source to share
Without real measure, I'm going to take a chance and argue that using boost::function
(or std::function
from C ++ 11) cannot be as efficient as the other two options.
The reason is that it function
uses type erasure to remove the type of the actual functor being used, which means that it is function
necessary to preserve the actual object that makes the call through the pointer and uses the function calls. On the other hand, in the other two methods, the compiler can inline the logic and remove the shipping cost.
This is actually very similar to the many times mentioned difference in performance of the C library qsort
versus C ++ sort
, where by using a functor the compiler has a better chance of embedding and optimization.
Then another question arises: does this affect your application, and for that you need to measure. Perhaps, in general, I / O cost or any other operation dominates your application and it won't make any difference.
source to share