Possible Duplicate:
Optimization due to constructor initializer list
OK, so here’s my dilemma : Let’s say we’ve got a class, along with several constructors, which are going to be called numerous times (some hundreds of millions of times per second; so speed is crucial).
Which way is preferable? (Is there any difference at all?)
Way A :
// Prototype
class MyClass
{
public:
// Constructor
MyClass (int x, int y, int z) : X(x), Y(y), Z(z) {}
// Variables
int X,Y,Z;
};
Way B :
// Prototype
class MyClass
{
public:
// Constructor
MyClass (int x, int y, int z);
// Variables
int X,Y,Z;
};
// Implementation
MyClass::MyClass(int x,int y,int z)
{
this->X=x;
this->Y=y;
this->Z=z;
}
You need to use profile tool to measure it by yourself. With build-in types maybe there is no difference (regarding construction speed) but for class types, using a member initializer list is always preferable way.