Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Is there an overhead in this OO design ?

Author: Mathieu Pagé

Date: 11:47:09 01/05/06

Go up one level in this thread


On January 05, 2006 at 14:13:07, Dann Corbit wrote:

>On January 05, 2006 at 13:04:29, Mathieu Pagé wrote:
>
>>Hi, I want to push OO to it's limit in order to get cleaner code. Here is what I
>>want to do :
>>
>>
>>class CSquare
>>   {
>>   private:
>>      unsigned int m_uiSquareIndex;
>>   public :
>>      // the next 3 functions allow CSquare to be used as an unsigned int in
>>      // arithmetic operations.
>>      inline CSquare(unsigned int);
>>      inline CSquare operator=(unsigned int);
>>      inline operator unsigned int();
>>
>>      // The next 2 functions are why i'd like to use OOP to make the
>>manipulation
>>      // of squares clearer.
>>      unsigned int GetColumn()
>>      {
>>         return m_uiSquareIndex % 8;
>>      };
>>
>>      unsigned int GetRow()
>>      {
>>         return m_uiSquareIndex / 8;
>>      };
>>   };
>>
>>
>>This way I can use CSquare like this :
>>
>>
>>CSquare csq(A1)
>>csq += 8; // One row higher. csq is now equat to A2.
>>csq.GetRow(); // Will return 1 (0 based index)
>>csq.GetColumn(); // will return 0
>>
>>
>>
>>I think that with basic compiler optimisations like inlining this code will
>>bring no overhead in my engine. I already asked some friends about it and they
>>seem to think like me, but are not sure.
>>
>>Since it's CC related and there is some good programmers monitoring this board I
>>though I would ask here.
>>
>>What is your opinion about this?
>>
>>Mathieu Pagé
>
>There may be some tiny benefit to:
>
>      unsigned int GetColumn()
>      {
>         return m_uiSquareIndex & 7; // Assumes 2's complement.
>      }
>
>      unsigned int GetRow()
>      {
>         return m_uiSquareIndex >> 3; // Assumes m_uiSquareIndex is >= 0
>      }
>
>But the compiler may actually do those simple optimizations for you anyway.
>
>I don't see any important overhead in your class.  Things like virtual functions
>and RTTI, structured exception handling, etc. add a little bit.
>
>By far, the algorithms chosen are more important than some little details about
>using C++ features.

Hi Dann, thanks for your answer,

I did some test and it seem there is no overhead to my class, at least in my
simple test function.

I know that the algorithms are more important than micro-optimisations, but I
just like thoses little hack that give a 0.01% performance boost to my engine.
Not that they are usefull performance amelioration, but I like to tweak code and
do micro optimisations. Hey ! CC programming is a hobby after all :)

I also looked at the two optimizations you provide for the modulo and division,
my compiler seem to do them for me and I think any other decent compiler will
do.

Mathieu Pagé



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.