Does the declaration contain C ++ variables or slow performance?

I understand the behavior of const -qualified data types. I am curious, however, if there is any profit or loss of performance from the excessive or perplexing qualifying variables as const . I think, in particular, about variables declared and used exclusively in an isolated block of code. 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 for all of these (and more). Is there any performance gain when declaring them all const ? Or, conversely, does it really degrade performance?

I'm curious about both runtime performance (I assume it doesn't matter since const is just a compile-time check - can someone confirm?) And compilation performance. I do not have enough experience with C ++ to understand this, and I wonder if I should be mistaken on the side of over- or under-apply const , when all other things (maintainability, etc.) are equal.

+6
c ++ performance const
source share
5 answers

const is basically compilation, however declaring something as const sometimes allows for certain optimizations. If the code in question is not a performance bottleneck, I would not have to worry about it and just used const as intended: to create clearer code and not allow myself to do stupid things.

+21
source share

My understanding is that const can be used by the compiler to potentially optimize performance, but this is not a guarantee; however, there should be no performance degradation. This can potentially affect runtime behavior (i.e., the compiler can put constant variables on read-only memory pages).

It should not have a significant impact on performance, but I would be mistaken for using it to simplify code maintenance. Just my opinion.

+2
source share

In my (limited) experience, const can severely damage performance (surprise!) Namely, when working with container classes, be careful what contress is: for arrays, this can just make the compiler create a copy of your container (for example, an array), like only one item will be readable only ... It was a huge pain to find in the code I'm working on.

+2
source share

While the answer is technically yes, the practical answer is NO. It is true that under certain circumstances, the compiler can perform code optimization, given that this value cannot change or that the method will not change the owner object. However, it 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.

+1
source share

const is here to help you catch errors at compile time. However, since this thing is called const_cast , you can always change the constant of any variable so that the compiler really cannot leave with optimization of something. (You can also enjoy c-style casts to get rid of a constant that might invalidate the optimization.)

0
source share

All Articles