Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Q: Performance for public vs protected data in C++ (OT)

Author: Bo Persson

Date: 15:00:47 10/21/02

Go up one level in this thread


On October 21, 2002 at 06:27:09, Gerd Isenberg wrote:

>On October 18, 2002 at 15:34:12, Bo Persson wrote:
>
>>On October 18, 2002 at 09:11:30, Gerd Isenberg wrote:
>>
>>>On October 17, 2002 at 19:29:30, Peter Fendrich wrote:
>>>
>>>>Is there any reason why 'protected' data should have better performance than
>>>>'public'? What could the compier do with this extra information?
>>>>Peter
>>>
>>>Hi Peter,
>>>
>>>access qualification has no impact on performance. Another thing are 'const'
>>>functions. The information that a function does not change anything inside this
>>>object permits the compiler to do additional optimizations, eg. holding data
>>>members in registers.
>>>
>>>Gerd
>>
>>No!
>>
>>The compiler can see for itself whether you change the data or not. If you
>>declare a member function const and then try to change the object anyway, the
>>compiler complains. Because it can tell!
>>
>>
>>
>>
>>Bo Persson
>>bop2@telia.com
>
>
>from AMD Athlon™ Processor x86 Code Optimization Guide:
>Chapter 3 C Source-Level Optimizations
>
>Use Const Type Qualifier
>Use the “const” type qualifier as much as possible. This
>optimization makes code more robust and may enable higher
>performance code to be generated due to the additional
>information available to the compiler. For example, the C
>standard allows compilers to not allocate storage for objects
>that are declared “const” if their address is never taken.
>
>Gerd

The standard allows the compiler to not allocate storage for *any* object, if
you can't tell the difference. It might keep the value in registers for a
variable's total lifetime. It doesn't matter whether it is declared 'const' or
not, because the compiler can tell if the value changes.

Consider compiler warnings like "unused parameter", "x is assigned a value, but
never used", "y is used before assigned a value". The compiler knows all about
the values, and is telling *you* what happens!


It is a good idea *anyway* to declare a value 'const', because then the compiler
will tell you if you accidentally tries to change it. It doesn't affects the
generated code though.


Bo Persson
bop2@telia.com





This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.