5

I have class with 3 member variables declared as public, I can initially it explicitly anywhere in code, still I have written constructor with initial values does this constructor affect performance overhead?

class ABC{
    public:
    int a;
    int b;
    int c;

    ABC (): a(0) , b(0), c(0) 
    {
    }
};

Please let me know if constructor add performance overhead?

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
  • 6
    Unless you're calling the constructor millions of times per second then it's irrelevant. – Paul R Aug 28 '13 at 16:50
  • 1
    If the compiler is able to prove that you always explicitly initialize it on all code-paths afterwards, then the compiler should be able to optimize it out as dead-assignments. Likewise, copy-elision should kick in if you assign it on all subsequent code-paths. – Mysticial Aug 28 '13 at 16:51

5 Answers5

6

The initialization will likely incur a small cost. However:

  1. The compiler might be able to eliminate the initializations if it can prove that they are unnecessary.

  2. Even if there is a small cost, it is overwhelmingly likely that it is completely irrelevant in the context of the overall application. You could use a profiler to quantify the performance effect.

  3. It gives you the reassurance of knowing that the three fields will always get initialized, thereby eliminating some types of potential bugs.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
3

Yes and no.

Yes, it does add some performance overhead, because you ask the computer to do some operations whereas in the default case it will not initialize members of basic types.

No, it does not add performance overhead in practice, because the operation will take an insignificant amount of time. Moreover, you need to initialize your fields anyway at some time (you'll never work with uninitialized fields, will you?). Hence, you'll only pay practical performance overhead when you need to change the initial values. But you could achieve the right initial values by defining a second constructor (one which takes parameters), and you probably should, so that you would avoid the default constructor call when you're not interested in it and instead, call a constructor which leaves your object initialized exactly as you want it.

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
1

It has roughly the exact same performance as this:

int a = 0;
int b = 0;
int c = 0;

Meaning the performance impact is so entirely negligible you shouldn't ever worry about it.

rossipedia
  • 56,800
  • 10
  • 90
  • 93
0

It initialises the int's to zero, which is prob good and take a smalll amount of time.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
0

For the general question does constructor affect performance, the answer is it depends.

  • In general, you want to use the initializer list when you can (otherwise you may be incurring on a default constructor and then a copy assignment, refer to this question for further explanation).

  • If you provide a non-throwing move constructor (i.e. noexcept(true)) operations like push_back into a container will use such (presumably cheap) constructor (otherwise the operation will copy values, presumably more expensive).

I am sure that others can come up with other reasons.

Finally, I would focus at this point in getting something working. If you then determine (after appropriate profiling) that your constructors are a bottleneck (I highly doubt it), then worry about improving them. Otherwise you may be wasting your time in utterly irrelevant nano-optimizations.

Note:

I made TWO big mistakes in answering this questions. I have removed them from the answer. Please see this comment's history to find out more.

Community
  • 1
  • 1
Escualo
  • 40,844
  • 23
  • 87
  • 135
  • Really? Are you reading another specification than me? Can you please refer to the exact section of the standard that states this, because I'd like to understand how you came to this conclusion. – Mats Petersson Aug 28 '13 at 17:09
  • That's not what the standard says, nor what most compilers do. The variables are left uninitialized, and any attempt to read from them is undefined behavior. – James Kanze Aug 28 '13 at 17:10
  • 1
    The opposite conclusion is easy draw: C++ draft n3290, §8.5, ¶11: "*If no initializer is specified for an object, the object is default-initialized; if no initialization is performed, an object with automatic or dynamic storage duration has indeterminate value.*" – Robᵩ Aug 28 '13 at 17:10
  • This is what I am referring to: "If no initializer is specified for an object, the object is default-initialized; if no initialization is performed, an object with automatic or dynamic storage duration has indeterminate value. [Note: Objects with static or thread storage duration are zero-initialized, see 3.6.2. — end note ]" – Escualo Aug 28 '13 at 17:12
  • In reviewing my answer I am affraid that this applies only to static variables... let me check further... – Escualo Aug 28 '13 at 17:14
  • I'm dumb (not enough coffee today...) I will re-do the answer. – Escualo Aug 28 '13 at 17:26
  • What I learned: you guys are right: it is by no means guaranteed to work (`int` being a built-in type). Thanks for keeping me honest. – Escualo Aug 28 '13 at 17:30