Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimization due to constructor initializer list

Constructors should initialize all its member objects through initializer list if possible. It is more efficient than building the constructors via assignment inside the constructor body.

Could someone explain, why it is more efficient to use the initializer list with the help of an example?

like image 863
nitin_cherian Avatar asked Dec 05 '11 14:12

nitin_cherian


People also ask

What is a constructor initializer list?

Initializer List is used in initializing the data members of a class. The list of members to be initialized is indicated with constructor as a comma-separated list followed by a colon. Following is an example that uses the initializer list to initialize x and y of Point class.

Does initializer list run before constructor?

The initializer list is used to directly initialize data members of a class. An initializer list starts after the constructor name and its parameters.

Does initializer list call constructor?

An initialization list can be used to explicitly call a constructor that takes arguments for a data member that is an object of another class (see the employee constructor example above). In a derived class constructor, an initialization list can be used to explicitly call a base class constructor that takes arguments.

Is initializer list faster?

Conclusion: All other things being equal, your code will run faster if you use initialization lists rather than assignment.


2 Answers

Consider this program:

#include <iostream>

struct A {
  A() { std::cout << "A::A()\n"; }
  A(int) { std::cout << "A::(int)\n"; }
  void operator=(const A&) { std::cout << "A::operator=(const A&)\n"; }
};

struct C1 {
  A a;
  C1(int i) { 
    a = i;
  }
};

struct C2 {
  A a;
  C2(int i)  : a(i) {}
};

int main() {
  std::cout << "How expesive is it to create a C1?\n";
  { C1 c1(7); }
  std::cout << "How expensive is it to create a C2?\n";
  { C2 c2(7); }
}

On my system (Ubuntu 11.10, g++ 4.6.1), the program produces this output:

How expesive is it to create a C1?
A::A()
A::(int)
A::operator=(const A&)
How expensive is it to create a C2?
A::(int)

Now, consider why it is doing that. In the first case, C1::C1(int), a must be default-constructed before C1's constructor can be invoked. Then it is must assigned to via operator=. In my trivial example, there is no int assignment operator available, so we have to construct an A out of an int. Thus, the cost of not using an initializer is: one default constructor, one int constructor, and one assignment operator.

In the second case, C2::C2(int), only the int constructor is invoked. Whatever the cost of a default A constructor might be, clearly the cost of C2:C2(int) is not greater than the cost of C1::C1(int).


Or, consider this alternative. Suppose that we add the following member to A:
void operator=(int) { std::cout << "A::operator=(int)\n"; }

Then the output would read:

How expesive is it to create a C1?
A::A()
A::operator=(int)
How expensive is it to create a C2?
A::(int)

Now is is impossible to say generally which form is more efficient. In your specific class, is the cost of a default constructor plus the cost of an assignment more expensive than a non-default constructor? If so, then the initialization list is more efficient. Otherwise it isn't.

Most classes that I've ever written would be more efficiently initialized in an init list. But, that is a rule-of-thumb, and may not be true for every possible case.

like image 69
Robᵩ Avatar answered Sep 16 '22 14:09

Robᵩ


Well, otherwise you call the default constructor and then perform an assignment. That's one step longer and may get really inefficient depending on the nature of initialization.

like image 24
Michael Krelin - hacker Avatar answered Sep 19 '22 14:09

Michael Krelin - hacker