c ++:模板类vs两类:效率

时间:2014-11-25 09:13:48

标签: c++ performance templates c++11

我尝试了两件事:

class RandDouble{
    public:
        RandDouble(double const& min_inclusive, double const& max_exclusive):
              mt_(std::random_device()),
              dist_(min_inclusive,max_exclusive)
         {}
        ~RandDouble(){}
        double get(){ return dist_(mt_); }

    private:
        std::mt19937_64 mt_;
        std::uniform_real_distribution<double> dist_;
};

class RandUnsignedInt{
    public:
        RandUnsignedInt(unsigned int const& min_inclusive, unsigned int const& max_inclusive):
              mt_(std::random_device()),
              dist_(min_inclusive,max_exclusive)
         {}
       ~RandUnsignedInt(){}
        unsigned int get(){ return dist_(mt_); }

    private:
        std::mt19937_64 mt_;
        std::uniform_int_distribution<unsigned int> dist_;
};

template<typename Type>
class Rand{
    public:
        Rand(Type const& min_inclusive, Type const& max_exclusive);/
        ~Rand();
        Type get();

    private:
        std::mt19937_64 mt_;
        std::uniform_real_distribution<double>* dist_double_;
        std::uniform_int_distribution<unsigned int>* dist_u_int_;
};
template<typename Type>
Rand<Type>::~Rand(){
    if(dist_double_){ delete dist_double_; }
    if(dist_u_int_){ delete dist_u_int_; }
}

使用.cpp文件:

template<>
Rand<double>::Rand(double const& min_inclusive, double const& max_exclusive):
    mt_(std::random_device()()),
    dist_double_(new std::uniform_real_distribution<double>(min_inclusive,max_exclusive)),
    dist_u_int_(NULL)
{}

template<>
Rand<unsigned int>::Rand(unsigned int const& min_inclusive, unsigned int const& max_exclusive):
    mt_(std::random_device()()),
    dist_double_(NULL),
    dist_u_int_(new std::uniform_int_distribution<unsigned int>(min_inclusive,max_exclusive))
{}

template<>
double Rand<double>::get(){ return (*dist_double_)(mt_); }

template<>
unsigned int Rand<unsigned int>::get(){ return (*dist_u_int_)(mt_); }

从实际角度来看,模板解决方案对其他模板类更灵活,因为我可以做以下事情:

template<typename Type>
classs C{
    /*some code*/
    private:
        Rand<Type> r;
};

所以我喜欢模板解决方案。但当我检查调用Rand<double/unsigned int>::get()方法所需的时间时,我意识到它需要的时间比从RandDouble::get()RandUnisignedint::get()拨打电话多两倍。

有一种方法可以保持模板方法的灵活性,调用方法与具有两个不同类的方法一样有效。

2 个答案:

答案 0 :(得分:5)

问题可能是通过使用指向分发类的指针获得的间接性。尝试直接使用没有指针的类,或者更好地做类似

的事情
typename std::conditional<std::is_integral<Type>::value
                        , std::uniform_int_distribution<Type>
                        , std::uniform_real_distribution<Type> >::type _dist;

以便选择您需要的分发类型。 (这只是为了给你一个提示,类型检查肯定会有所改进)。


说明:上面的代码的工作方式如下:std::conditional<(1),(2),(3)>就像类型的静态if语句一样。如果第一个字段(1)中的签入计算结果为true,则会在第二个字段(2)中使用该类型,否则它会在第三个字段(3)中选择该类型。

如果模板参数Type是整数类型,std::is_integral<Type>::value将评估为true。因此,您的发行版类型通常是std::uniform_int_distribution<Type>

如果Type它不是一个整数类型(而是一个浮点类型,但是这里没有检查),而是std::uniform_real_distribution<Type>用于分布的类型。 / p>

示例(tested here):

#include<random>
#include<iostream>

template<typename Type>
struct UniformDistribution
{
    std::mt19937_64 mt_;
    typename std::conditional<std::is_integral<Type>::value
                            , std::uniform_int_distribution<Type>
                            , std::uniform_real_distribution<Type> >::type dist_;    
    Type get()
    {
        return dist_(mt_);
    }
};

int main()
{
     //produces uniformly distributed integer number in [0, numeric_limist<int>::max()]
     std::cout<<UniformDistribution<int>().get()<<std::endl;

     //produces uniformly distributed double number in [0,1]
     std::cout<<UniformDistribution<double>().get()<<std::endl;
}

答案 1 :(得分:2)

#include<random>

template< class T >
struct TDist{};

template<> struct TDist<double> { std::uniform_real_distribution<double> dist_; };
template<> struct TDist<unsigned int> { std::uniform_int_distribution<unsigned int> dist_; };


template<typename Type>
class Rand : private TDist<Type> {
public:
    Rand(Type min_inclusive, Type max_exclusive) :
        mt_(std::random_device()),
        dist_(min_inclusive,max_exclusive)
    {}

    Type get(){ return dist_(mt_); }

private:
    std::mt19937_64 mt_;
};