Qt 中哪个更有效:带有参数的构造函数或随后带有 setter 的默认构造函数?

发布于 2024-12-21 23:43:39 字数 414 浏览 2 评论 0原文

问题如标题所示。

例如:

QPropertyAnimation *animation;
animation = new QPropertyAnimation(this, "windowOpacity", this);

QPropertyAnimation animation;
animation.setTargetObject(this);
animation.setPropertyName("windowOpacity");
animation.setParent(this);

哪个更有效率?

编辑:虽然除非重复执行,否则没有显着差异,但我仍然想知道,我宁愿想要答案而不是意见 - 正如 stackoverflow 指南所建议的那样。

The question is as in the title.

For example:

QPropertyAnimation *animation;
animation = new QPropertyAnimation(this, "windowOpacity", this);

or

QPropertyAnimation animation;
animation.setTargetObject(this);
animation.setPropertyName("windowOpacity");
animation.setParent(this);

Which is more efficient?

edit: though it has no significant difference unless done repeatedly, i would still like to know, i would rather want answers than opinions -as stackoverflow's guidelines suggest.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苏大泽ㄣ 2024-12-28 23:43:39

首先,为什么在第一个示例中使用 new ?我假设您将在同一存储(堆/堆栈)上创建两个变量。

其次,这不是 Qt 的问题,它通常适用于 C++。

如果没有关于您正在创建的类的任何先验知识,您可以确定一件事:带参数版本的构造函数至少与 setter 版本一样高效。

这是因为,在最坏的情况下,构造函数可能看起来像这样:

QPropertyAnimation(QObject* target, const QByteArray & prop_name, QObject* parent = 0)
{
  // members are default initializer, now explicitly set
  this->setTargetObject(target);
  this->setPropertyName(prop_name);
  this->setParent(parent)
}

但是,任何至少读过一本好书的人都会像这样编写构造函数:

QPropertyAnimation(QObject* target, const QByteArray & prop_name, QObject* parent = 0)
  : m_target(target)
  , m_prop_name(prop_name)
  , m_parent(parent)
{
  // members explicitly initialized
}

First, why new in the first example? I'll assume that you will create both variables on the same storage (heap / stack).

Second, this isn't a matter of Qt, it applies to C++ in general.

Without any prior knowledge about the class you are creating, you can be sure of one thing: The constructor with arguments version is at least as efficient as the setter version.

This is because, in the worst case, the constructor might look like this:

QPropertyAnimation(QObject* target, const QByteArray & prop_name, QObject* parent = 0)
{
  // members are default initializer, now explicitly set
  this->setTargetObject(target);
  this->setPropertyName(prop_name);
  this->setParent(parent)
}

However, any person that has atleast worked through a good book will write the constructor like this:

QPropertyAnimation(QObject* target, const QByteArray & prop_name, QObject* parent = 0)
  : m_target(target)
  , m_prop_name(prop_name)
  , m_parent(parent)
{
  // members explicitly initialized
}
梦亿 2024-12-28 23:43:39

至于一次调用还是三次调用(好吧,2.5,因为第一个调用是隐式的)是否“更好”(忽略堆问题),值得考虑程序的概念流程以及您对它的智力控制。与编码相关的实际问题也值得考虑。

在调用方,如果创建对象的地方已经有了所有适当的参数,那么单个调用就更明显地表明,事实上,所有参数“属于”该对象,并且它是“在一个对象中创建的”。片”。另一方面,如果使用单个调用意味着调用代码必须随着时间的推移收集参数,然后吐出单个“被压抑”的调用,那么创建对象然后设置相应的属性可能是更好的选择随着他们价值观的发展,一次一个。

并且,在被调用方,可能还有实际的考虑。例如,可能有十几个属性,对象的不同用途可能会使用不同的组合。提供与多个属性设置器相结合的单个构造函数(或其中的一小部分),而不是提供数十个不同的构造函数,既可以更有效地节省程序员的时间,又不易使对象的用户感到困惑。但是,如果(几乎)总是使用相对较少数量的参数的相同组合,那么单个调用可能会更好地利用程序员资源。

(这里重要的是,C++ 没有实现真正的关键字参数,因此当参数列表超过 4-5 项时,人们会失去对哪个参数是哪个参数的智能控制,特别是在构造函数有多种形式的情况下。使用单独的属性设置器的情况会给出关键字参数的(粗略)效果并减少混淆的机会。)

效率并不总是与 CPU 周期有关。在许多方面,有效利用程序员的时间(包括减少调试时间)更为重要。

At to whether the one call or three (OK, 2.5, since the first call is implicit) is "better" (ignoring the heap issue), it's worthwhile thinking about the conceptual flow of the program, and your intellectual control over it. And it's also worth considering practical issues related to coding.

On the caller side, if all the appropriate parameters are already at hand where the object is being created, then the single call makes it more obvious that, indeed, all the parameters "belong" to that object, and it's being created "in one piece". On the other hand, if using a single call means that the calling code must gather up parameters over time and then spit out a single "pent up" call, then it may be a better choice to create the object and then set the corresponding properties one at a time, as their values are developed.

And, on the callee side, there may be practical considerations. For instance, it may be that there are a dozen properties, with different uses of the object likely to use different combinations. Rather than provide dozens of different constructors, providing a single constructor (or a small number of them) combined with multiple property setters is both more efficient of programmer time and less apt to be confusing to the user of the object. But if the same combination of a relatively small number of parameters is (almost) always used then the single call is probably a better use of programmer resources.

(Of some importance here is the fact that C++ does not implement true keyword parameters, so when parameter lists get beyond 4-5 items one loses intellectual control over which parameter is which, especially if there are several forms of the constructor. In such a case using separate property setters gives the (rough) effect of keyword parameters and reduces the chance of confusion.)

Efficiency isn't always about CPU cycles. Efficient use of programmer time (including reduced time spent debugging) is, in many ways, far more important.

风柔一江水 2024-12-28 23:43:39

在其他条件相同的情况下,一次函数调用优于 3 次。

All else being equal one function call is better than 3.

像极了他 2024-12-28 23:43:39

你正在比较苹果和橙子。在第一种情况下,您从堆构造一个对象,而在第二种情况下,您在另一个对象或自动存储中“就地”构造一个对象,因此没有堆开销。与您使用单个构造函数调用还是使用(隐式)构造函数加两个 setter 无关。

You're comparing apples and oranges. In the first case you're constructing an object from heap, while in the second case you're constructing an object "in place", in another object or in automatic storage, so there's no heap overhead. Has nothing to do with whether you use a single constructor call or a (implicit) constructor plus two setters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文