3

我试图确定boost::function引入的用于评估数学函数的时间开销与使用function templates.

我使用的基准代码如下。

使用传统g++的,开销boost::function可以忽略不计:

$ 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   

有了llvm-g++, 有 1.5 倍的速度增益templates function,但 没有 增益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    

是否有可能获得 和 的 1.5boost::function增益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());
}
4

1 回答 1

0

如果没有实际测量,我将冒险并声称使用boost::function(或std::function来自 C++11)不能像其他两个选项那样有效。

原因是function使用类型擦除来删除正在使用的实际仿函数的类型,这意味着function需要存储通过指针进行调用的实际对象并使用函数调用。另一方面,在其他两种方法中,编译器能够内联逻辑并消除调度成本。

这实际上与多次提到的 C 库qsort与 C++ 相比的性能差异非常相似sort,其中通过使用仿函数,编译器有更好的内联和优化机会。

那么另一个问题是,这是否会对您的应用程序产生影响,并且您需要对此进行衡量。可能是 IO 的总体成本或任何其他操作主导您的应用程序,这根本不会产生影响。

于 2012-03-15T13:17:56.293 回答