Can const-correctness improve performance?

前端 未结 4 521
清歌不尽
清歌不尽 2020-11-29 18:22

I have read numerous times that enforcing const-correctness in your C or C++ code is not only a good practice with regards to maintainability, but also it may allow your com

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 18:25

    in my experience, no

    For scalar variables, compiler is able to determine whenever the value is changed and perform necessary optimization itself.

    For array pointers, const correctness is no guarantee that values are really constant in presence of potential aliasing problems. Hence compiler can not use const modifier alone to perform optimizations

    if you are looking optimization, you should consider __restrict__ or special function modifiers/attributes: http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

提交回复
热议问题