是否声明 C++变量 cons 有助于还是损害性能?

发布于 2024-09-26 09:09:09 字数 750 浏览 1 评论 0原文

我了解 const 限定数据类型的行为。不过,我很好奇,将变量限定为 const 是否会带来任何性能增益或损失。我特别考虑在独立代码块中专门声明和使用的变量。例如,类似于:

const qreal padding = CalculatePadding();
const QSizeF page_size = CalculatePagePreviewSize(padding);
const QRectF content_rect = CalculatePagePreviewContentRect(page_size);
const QList<QRectF> pages = renderer.BuildPrintPages(printer_, map_scene_);
const QFont page_number_font = CalculatePageNumberFont();
const QFontMetrics metrics(page_number_font);

假设我只需要所有这些(以及更多)的 const 限定方法。将它们全部声明为 const 是否会带来性能提升?或者,相反,这实际上会损害性能吗?

我对运行时性能感到好奇(我猜这没有什么区别,因为 const 完全是编译时检查 - 有人可以确认吗?)和编译-时间表现。我没有足够的 C++ 经验来对此有感觉,并且想知道当所有其他事情(可维护性等)都在使用 const 时,我是否应该犯过度或不足应用的错误。是平等的。

I understand the behavior of const-qualified data types. I am curious, though, if there is any performance gain or loss from over- or under-zealousness of qualifying variables as const. I am thinking particularly of variables declared and used exclusively within an isolated code block. For example, something like:

const qreal padding = CalculatePadding();
const QSizeF page_size = CalculatePagePreviewSize(padding);
const QRectF content_rect = CalculatePagePreviewContentRect(page_size);
const QList<QRectF> pages = renderer.BuildPrintPages(printer_, map_scene_);
const QFont page_number_font = CalculatePageNumberFont();
const QFontMetrics metrics(page_number_font);

Suppose I only need const-qualified methods on all of these (and more.) Is there any performance gain in declaring them all const? Or, conversely, does this actually hurt performance?

I am curious for both run-time performance (I am guessing this makes no difference as the const is exclusively a compile-time check--can someone confirm?) and compile-time performance. I do not have enough experience with c++ to have a feel for this, and am wondering if I should err on the side of over- or under-applying const when all other things (maintainability, etc.) are equal.

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

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

发布评论

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

评论(6

白云悠悠 2024-10-03 09:09:09

const 主要是编译时的事情,但是,将某些内容声明为 const 有时可以实现某些优化。如果有问题的代码不是性能瓶颈,我不会担心它,只需按预期使用 const:生成更清晰的代码并防止自己做愚蠢的事情。

const is mainly a compile-time thing, however, declaring something as const sometimes allows for certain optimizations. If the code in question isn't a performance bottleneck, I wouldn't worry about it and just use const as intended: to produce clearer code and prevent yourself from doing stupid things.

回忆追雨的时光 2024-10-03 09:09:09

根据我(有限的)经验, const 会对性能造成很大影响(惊讶!)
也就是说,在使用容器类时要小心 cont-ness 的作用:对于数组,一旦访问单个元素以只读方式,它可能会强制编译器创建容器的副本(例如,数组)...这是在我正在处理的代码中定位是一个巨大的痛苦。

In my (limited) experience, const CAN hurt the performance by quite a lot (surprise!)
Namely, when working with container classes be careful what cont-ness does: for arrays it might simply force the compiler to create a copy of your container (e.g., an array) once a single element is accessed for reading only... This was a huge pain to locate in the code that I am working on.

圈圈圆圆圈圈 2024-10-03 09:09:09

我的理解是编译器可以使用 const 来潜在地优化性能,但不能保证这一点;不过,不应该有性能下降。它可能会影响运行时行为(即:编译器可以将 const 变量放在只读内存页上)。

它不应该对性能产生重大影响,但我错误地更多地使用它以方便代码维护。不过,这只是我的意见。

My understanding is that const can be used by the compiler to potentially optimize performance, but is no guarantee of such; there shouldn't be a performance downside, though. It could potentially affect runtime behavior (ie: the compiler could put const variables on read-only memory pages).

It should not have a significant impact on performance, but I'd error on using it more for ease of code maintenance. Just my opinion, though.

枉心 2024-10-03 09:09:09

是的,如果应用于对象本身,const 可以提高性能。

示例

int fmutable(void g(const int &)) {
    int x = 1;
    g(x);
    return x;  // mov rax, dword ptr [rsp + ...]
}

int fconst(void g(const int &)) {
    int const x = 1;
    g(x);
    return x;  // mov eax, 1
}

发生这种情况是因为改变了 const object 是非法的,因此编译器可以假设您没有这样做。

但是,将 const 添加到之前可变的引用指针的目标并不会提高性能,因为放弃 const 然后改变原本不是 const 的对象是合法的,并且编译器不能假设您不这样做。

Yes, const can improve performance if applied to the object itself.

Example:

int fmutable(void g(const int &)) {
    int x = 1;
    g(x);
    return x;  // mov rax, dword ptr [rsp + ...]
}

int fconst(void g(const int &)) {
    int const x = 1;
    g(x);
    return x;  // mov eax, 1
}

This happens because mutating a const object is illegal, so the compiler can assume you don't do that.

However, adding const to the target of a reference or pointer that was previously mutable doesn't improve performance, because casting away const and then mutating an object that wasn't originally const is legal, and the compiler cannot assume you don't do that.

伴我老 2024-10-03 09:09:09

虽然从技术上来说答案是“是”,但实际答案是否定的。确实,在某些情况下,编译器可以通过考虑给定值无法更改或方法不会修改所属对象来执行代码优化。然而,这些都是情境情况,并且在优化的杂草中是如此令人难以置信,以至于预先考虑它几乎肯定是一个错误。

While the answer is technically "yes", the practical answer is NO. It's true that the compiler can, under certain circumstances, perform code optimizations by taking into account that a given value cannot change or that a method will not modify the owning object. However, these will be situational cases and so incredibly far down in the weeds of optimization that it would almost certainly be a mistake to take it into account up front.

只是我以为 2024-10-03 09:09:09

const 只是帮助您在编译时捕获错误。然而,由于有一个叫做 const_cast 的东西,您总是可以更改任何变量的常量,因此编译器实际上无法逃避任何优化。 (您还可以使用 c 风格的强制转换来消除常量,这可能会使优化无效。)

const is just there to help you catch errors at compile time. However since there's this thing called the const_cast you can always change the constness of any variable so the compiler really can't get away with optimizing anything away. (You can also have fun with c-style casts to get rid of constness which could make optimizations invalid.)

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