Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: C and C++ --- NPS

Author: Matt Taylor

Date: 10:32:42 12/24/02

Go up one level in this thread


On December 24, 2002 at 05:29:47, David Rasmussen wrote:

>On December 23, 2002 at 21:33:50, Matt Taylor wrote:
>
>>
>>The biggest hit you take in C++ is virtual functions, and inevitably people use
>>them. The virtual functions are very damaging due to the fact that they're
>>implemented with a function pointer, and at the machine level it translates into
>>an indirect call. The indirect calls branch mispredict because the CPU can't
>>figure out ahead-of-time where you're calling to, and branch mispredicts are
>>expensive.
>>
>>Unfortunately, not using virtual functions (mostly) defeats the benefits you
>>would get from OO here.
>>
>
>First of all, that is a poor understanding of OO.
>Secondly, OO isn't the only virtue of C++. By far.
>
>You would only use virtual functions when you need polymorphism. And if you
>wanted to do that without virtual functions, you would need some other form of
>dispatch such as a switch statement. Many different tests shows that a function
>call and a switch statement isn't faster than a virtual function call. In fact
>it is slower.
>All of this doesn't matter, as there is no obligation to use virtual functions
>in a chess program to use C++ successfully.

I am aware of the virtues of C++. I have been programming in C++ for the past 10
or 11 years. Not as long as some, but plenty long enough to be intimately
familiar with the language.

My point was that polymorphism would be the main benefit for a chess engine. I
don't see any particularly clever use for templates or operator overloading
here. Of course, some will always do it because they can, even if it's a bad
idea.

A switch statement will always be faster if the compiler is any good at
optimizing it. The difference is that it avoids extra stack usage, function call
setup costs (preserving registers), and in some cases you can get a branch
prediction hit.

>>If you convert to C++ without using virtual functions, you probably won't take
>>much of a hit at all. Personally, I've never seen a -need- to convert to C++.
>
>I see no need to use an inferior language to express the same thing for no gain
>at all. C++ gives my type safety, better I/O and a lot more.

Better I/O? You mean you like the I/O library better? I find printf-style easier
to read. I've never had a problem with %f bugs in my code because I don't pass a
user string into printf directly.

In terms of "good language" vs. "bad language", C++ really maintains no
advantage over C. Someone who wants a "good language" can write their code in
Ada. I will write mine in C.

>>I
>>can implement my OO in C, and it simplifies linking because C doesn't have name
>>mangling.
>>
>
>Is that an issue? It hasn't been an issue for me at all.
>
>/David

It's a pain to mix with assembly, C, and other languages. It's also a pain to
put in a DLL/shared object and use in other applications.

-Matt



This page took 0.02 seconds to execute

Last modified: Thu, 07 Jul 11 08:48:38 -0700

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