简体   繁体   English

构造函数会影响性能吗?

[英]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? 我有3个成员变量声明为public的类,我最初可以在代码中的任何地方显式显示,我仍然使用初始值编写构造函数这个构造函数会影响性能开销吗?

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). 如果你提供一个非投掷移动构造函数(即noexcept(true) ),像push_back这样的操作将使用这样的(大概是便宜的)构造函数(否则操作将复制值,可能更昂贵)。

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. 请查看此评论的历史记录以了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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