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

后端 未结 5 1209
爱一瞬间的悲伤
爱一瞬间的悲伤 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:25

    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.

提交回复
热议问题