Using std :: function to improve the inefficiencies of template

Generic programming, the template will be different according to the type of incoming generates various examples, relatively inefficient.

Template Programming:

#include <iostream>

using namespace std;

//未使用函数包装器
template <typename T,typename F>
T use_f(T v, F f)
{
    static int count = 0;
    count++;
    cout<<"use_f count = "<<count<<": &count"<<&count<<endl;
    return f(v);
}


class Fp
{
private:
    double z_;
public:
    Fp(double z = 1.0):z_(z){}
    double operator ()(double p) {return z_*p;}
};


class Fq
{
private:
    double z_;
public:
    Fq(double z = 1.0):z_(z){}
    double operator ()(double p) {return z_+p;}
};

double dub(double x){return 2.0*x;}
double square(double x){return x*x;}


int main(int argc, char *argv[])
{
    double y = 1.2;
    cout<<"Function pointer dub:"<<endl;
    cout<<" --"<<use_f(y,dub);
    cout<<"Function pointer square:"<<endl;
    cout<<" --"<<use_f(y,square);
    cout<<"Function pointer Fp:"<<endl;
    cout<<" --"<<use_f(y,Fp());
    cout<<"Function pointer Fq:"<<endl;
    cout<<" --"<<use_f(y,Fq());
    cout<<"Function pointer lambada1:"<<endl;
    cout<<" --"<<use_f(y,[](double u){return u*u;});
    cout<<"Function pointer lambada2:"<<endl;
    cout<<" --"<<use_f(y,[](double u){return u+2.5;});
    return 0;
}

 

 Template programming advantages are highly abstract, the algorithm unified package, but depending on the type instantiated properties also caused their inefficiency.

 Results of the above, by viewing the static template functions, static data addresses, which generates five function instance. The amount of code generated a huge increase affected incoming type.

 

To reduce by std :: function template instantiation:

#include <iostream>
#include <functional>
using namespace std;

//使用函数包装器
template <typename T,typename F>
T use_f(T v, F f)
{
    static int count = 0;
    count++;
    cout<<"use_f count = "<<count<<": &count"<<&count<<endl;
    return f(v);
}


class Fp
{
private:
    double z_;
public:
    Fp(double z = 1.0):z_(z){}
    double operator ()(double p) {return z_*p;}
};


class Fq
{
private:
    double z_;
public:
    Fq(double z = 1.0):z_(z){}
    double operator ()(double p) {return z_+p;}
};

double dub(double x){return 2.0*x;}
double square(double x){return x*x;}


int main(int argc, char *argv[])
{
    double y = 1.2;
    //    cout<<"Function pointer dub:"<<endl;
    //    cout<<" --"<<use_f(y,dub);
    //    cout<<"Function pointer square:"<<endl;
    //    cout<<" --"<<use_f(y,square);
    //    cout<<"Function pointer Fp:"<<endl;
    //    cout<<" --"<<use_f(y,Fp());
    //    cout<<"Function pointer Fq:"<<endl;
    //    cout<<" --"<<use_f(y,Fq());
    //    cout<<"Function pointer lambada1:"<<endl;
    //    cout<<" --"<<use_f(y,[](double u){return u*u;});
    //    cout<<"Function pointer lambada2:"<<endl;
    //    cout<<" --"<<use_f(y,[](double u){return u+2.5;});

    std::function<double(double)> fdub     = dub;
    std::function<double(double)> fsquare  = square;
    std::function<double(double)> fFp      = Fp();
    std::function<double(double)> fFq      = Fq();
    std::function<double(double)> lambada1 = [](double u){return u*u;};
    std::function<double(double)> lambada2 = [](double u){return u+2.5;};

    cout<<"Function pointer dub:"<<endl;
    cout<<" --"<<use_f(y,fdub);
    cout<<"Function pointer square:"<<endl;
    cout<<" --"<<use_f(y,fsquare);
    cout<<"Function pointer Fp:"<<endl;
    cout<<" --"<<use_f(y,fFp);
    cout<<"Function pointer Fq:"<<endl;
    cout<<" --"<<use_f(y,fFq);
    cout<<"Function pointer lambada1:"<<endl;
    cout<<" --"<<use_f(y,lambada1);
    cout<<"Function pointer lambada2:"<<endl;
    cout<<" --"<<use_f(y,lambada2);

    return 0;
}

 

 

所有的静态变量只有一个地址,也就是说模板 函数只产生了一个实例,其类型只需要匹配是一个std::function<double(double)>传入的对象即可,模板的效率大为提高。 

但是看来好像代码量并未减少甚至还有些许增多,下面我们通过优化来解决这个问题。

 

熊掌与鱼:

#include <iostream>
#include <functional>
using namespace std;

//模板中使用函数包装器
template <typename T>
T use_f(T v, std::function<T(T)> f)
{
    static int count = 0;
    count++;
    cout<<"use_f count = "<<count<<": &count"<<&count<<endl;
    return f(v);
}


class Fp
{
private:
    double z_;
public:
    Fp(double z = 1.0):z_(z){}
    double operator ()(double p) {return z_*p;}
};


class Fq
{
private:
    double z_;
public:
    Fq(double z = 1.0):z_(z){}
    double operator ()(double p) {return z_+p;}
};

double dub(double x){return 2.0*x;}
double square(double x){return x*x;}


int main(int argc, char *argv[])
{
    double y = 1.2;
    //这里需要<double>使得std::function<T(T)>实例化为具体的对象
    cout<<"Function pointer dub:"<<endl;
    cout<<" --"<<use_f<double>(y,&dub);
    cout<<"Function pointer square:"<<endl;
    cout<<" --"<<use_f<double>(y,square);
    cout<<"Function pointer Fp:"<<endl;
    cout<<" --"<<use_f<double>(y,Fp());
    cout<<"Function pointer Fq:"<<endl;
    cout<<" --"<<use_f<double>(y,Fq());
    cout<<"Function pointer lambada1:"<<endl;
    cout<<" --"<<use_f<double>(y,[](double u){return u*u;});
    cout<<"Function pointer lambada2:"<<endl;
    cout<<" --"<<use_f<double>(y,[](double u){return u+2.5;});



    return 0;
}

 

 

 实例化与代码量都减少了,鱼和熊掌兼得。

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/wangkeqin/p/11946255.html
Recommended