변수의 const 사용으로 성능을 향상 시킬수있을까?


link : http://stackoverflow.com/questions/3435026/can-const-correctness-improve-performance



const correctness can't improve performance because const_cast and mutable are in the language, and allow code to conformingly break the rules. This gets even worse in C++11, where your const data may e.g. be a pointer to a std::atomic, meaning the compiler has to respect changes made by other threads.

That said, it is trivial for the compiler to look at the code it generates and determine if it actually writes to a given variable, and apply optimizations accordingly.

That all said, const correctness is a good thing with respect to maintainability. Otherwise, clients of your class could break that class's internal members. For instance, consider the standard std::string::c_str() -- if it couldn't return a const value, you'd be able to screw around with the internal buffer of the string!

Don't use const for performance reasons. Use it for maintainability reasons.


google 번역

const 정확성은 const_cast 및 mutable이 언어에 있기 때문에 성능을 향상시킬 수 없으며 코드가 규칙을 준수하여 규칙을 위반하지 않도록합니다. 이것은 C ++ 11에서 더욱 악화됩니다. const 데이터는 예를 들어. std :: atomic에 대한 포인터가됩니다. 즉, 컴파일러는 다른 스레드가 변경 한 내용을 존중해야합니다.


즉, 컴파일러가 생성하는 코드를보고 실제로 주어진 변수에 쓰는지 여부를 판별하고 이에 따라 최적화를 적용하는 것은 간단합니다.


즉, const 정확성은 유지 보수성 측면에서 좋은 점입니다. 그렇지 않으면 클래스의 클라이언트가 해당 클래스의 내부 구성원을 손상시킬 수 있습니다. 예를 들어, 표준 std :: string :: c_str ()을 고려해보십시오. const 값을 반환 할 수없는 경우 문자열의 내부 버퍼를 사용할 수 있습니다.


성능상의 이유로 const를 사용하지 마십시오. 유지 보수상의 이유로 사용하십시오.



'프로그래밍' 카테고리의 다른 글

c++11 타입추론 auto, decltype  (0) 2015.10.28
PostQuitMessage DestroyWindow 차이  (0) 2015.10.15
ubuntu 시스템 언어 변경  (0) 2015.08.26
git 소개  (0) 2015.08.21
Visual Assist X 소개  (0) 2015.08.21