[Optimisation] Meta-prog deux fois moins vite et compilation
Bonjour,
Je m'intéresse à la meta-prog pour optimiser des calculs entre des vecteurs(class Point). Cependant je remarque que le calcul fait sur une recursion et specialisation pour boucler va deux moins vite que d'écrire directement le code.
Est-ce normal? Ai-je mal codé?
Merci de vos réponse.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
template <int Begin, int End> struct Boucle
{
template<class T>
static void Add( T & p1, const T &p2)
{
p1[Begin]+=p2[Begin];
Boucle<Begin+1,End>::Add(p1,p2);
}
};
template <int N> struct Boucle<N, N>
{
template<class T>
static void Add( T & p1, const T &p2) {}
};
template <int dim, class T>
class Point
{
public:
T dat[dim];
inline Point<dim,T> & Add(const Point<dim,T>& p)
{
Boucle<0, dim>::Add(*this, p);
return *this;
}
inline Point<dim,T> & Addf(const Point<dim,T>& p)
{
(*this)[0]+=p[0];
(*this)[1]+=p[1];
(*this)[2]+=p[2];
(*this)[3]+=p[3];
return *this;
}
inline const T & operator []( int i)
const
{
return this->dat[i];
}
inline T & operator []( int i)
{
return this->dat[i];
}
};
int main(void){
Point<4,int > p1,p2;
int t1=time(NULL);
for(int i=0;i<100000;i++)
for(int j=0;j<1000;j++)
{
p1.Add(p2);
}
int t2=time(NULL);
cout<< t2-t1<<endl;
t1=time(NULL);
for(int i=0;i<100000;i++)
for(int j=0;j<1000;j++)
{
p1.Addf(p2);
}
t2=time(NULL);
cout<< t2-t1<<endl;
} |
Affichage console: