简体   繁体   中英

Does constructor affect performance?

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?

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.

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.

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.

它将int初始化为零,这可能是好的并且需要花费很少的时间。

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 (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM