Computer Chess Club Archives


Search

Terms

Messages

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

Author: Vincent Diepeveen

Date: 08:11:38 12/24/02

Go up one level in this thread


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

It's very trivial that when you use high level language
constructions in c++ that it is way slower than C.

on the other hand it is possible to write a kind of C a look
like c++ which is trivially having the same speed like C.

I do not understand why people argue about that.

No C++ isn't faster. No C++ doesn't need to be slower. It's
basically the same language with c++ having a few constructions
that enable OO programming which are high level and very dead
slow to use.

The average student for example doesn't realize that object
allocation is very expensive and that things like overloading
inheritance and all kind of cool c++ constructions are dead slow.

The more you use of them the slower it gets. A factor 3 will be
not exceptional.

But the average neat written code is by definition a lot slower
than code written for speed.

I was now looking to some other products, nothing to do with chess
at the source code of some code which, written in C, and in total
around 2.5MB of source code for that part of the functionality.

Very neatly written C code. Very good readable.

Written for speed. Indeed it is using a lot of clever tables.

But a good C hacker will replace so many stuff with pointers
and it'll get at least 50% faster.

Same is true for C++. An excellent C program written for speed
is of course times faster than C++ code that uses typical C++
constructions and is not written for speed.

I do not see the problem here. This is a logical thing.

I had to chose between writing my GUI functionality (database and such)
in C or C++ before i started. Initially i wanted to start it in C++
but in the end i figured out that speed for a chess database is not
unimportant so i wrote it in C.

But i could of course have done it in C++ as well. In fact if i change
the extension of the code from *.c to *.cpp then it pretty quickly
is c++ code already, the nearly same code being both C and C++. Says
enough to me about speed differences.

None if well written.

>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.
>
>>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.
>
>>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



This page took 0.01 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.