Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Use of objects and associated performance hit?

Author: Bruce Moreland

Date: 23:38:20 12/30/99

Go up one level in this thread


On December 30, 1999 at 18:53:04, Tom Kerrigan wrote:

>Although I love C and I usually don't approve of object-oriented programming, I
>think there are compelling reasons to use objects in a chess program.
>
>Specifically, I want to have a chess board class, which contains the data and
>functions necessary to manipulate a chess board. I also want to have an engine
>class, which extends the chess board class, and adds the functions necessary to
>search the board.
>
>I have two questions:
>1. When I access the chess board class from the engine class, will there be a
>performance hit?
>2. If I have multiple instances of the engine class, can each one run on a
>different processor without a performance hit?

A class is just a struct whose members are by default private.  A struct's
members are by default public.  There's extra C++ nonsense involving member
functions and all, but you can actually do that stuff with structs, too.

So if you declare a class you are just making a struct, not some sort of
high-tech thing with lots of complex overhead.  You have to add the overhead
yourself, and you'd tend to know about it if you do.

How normal member functions work is they pass a pointer to the object as an
invisible first argument to the function.  So you have some parameter passing
overhead, which you may have ended up with anyway.  You can do a lot of this in
C by making a struct, then making a bunch of functions which take a pointer to
the struct as their first parameter, and then declaring one of these structs and
passing it around all over the place.  Which is probably what you end up doing
now, a lot of the time.

So whether or not this will hurt you depends upon how weird you get.  The kind
of overhead you get doing normal and simple C++ stuff isn't really worth
worrying about.

C++ allows you to make a pompous ass out of yourself easier than you can if you
program in C, but that's your choice, it's not forced upon you by the language.

A C++ purist can find much wrong with what I said above, there are all kinds of
instances where I am not completely correct.  But for someone who is trying to
do simple stuff with C++, what I said is correct enough.

If code clarity is improved, it is worth any kind of trivial performance hit.

bruce



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.