Does declaring C++ variables const help or hurt performance?

后端 未结 5 1208
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-19 04:45

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 quali

5条回答
  •  星月不相逢
    2020-12-19 05:21

    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.

提交回复
热议问题