c vs c++

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

c or c++ ?

c
24
48%
c++
26
52%
 
Total votes: 50

ethanara
Posts: 134
Joined: Mon May 16, 2011 6:58 pm
Location: Denmark

c vs c++

Post by ethanara »

Hi again
I was thinking which language is fastest at chess programming, c or c++ ?
I think it would be possible to make a c++ version of micromax and test result, or making a c version of stockfish
regards
Ethan Ara
User avatar
Evert
Posts: 2929
Joined: Sat Jan 22, 2011 12:42 am
Location: NL

Re: c vs c++

Post by Evert »

ethanara wrote:I was thinking which language is fastest at chess programming, c or c++ ?
I'd be surprised if it many any significant difference.
I think it would be possible to make a c++ version of micromax and test result, or making a c version of stockfish
A waste of time, I expect.
To get a C++ version of micromax, simply compile it using a C++ (rather than a C) compiler. A C version of of Stockfish will be a LOT of work, since it makes fairly intensive use of templates. And again, I suspect it's a waste of time.
rbarreira
Posts: 900
Joined: Tue Apr 27, 2010 3:48 pm

Re: c vs c++

Post by rbarreira »

They both allow inline assembly so they're equally fast :P

More seriously, since C++ is for all practical purposes a superset of C, it cannot possibly be slower than C assuming equally good compilers.

This of course doesn't mean it's worth it to use the extra features C++ has in order to get better performance, in fact some of them can be counter productive for performance. On the other hand some people have success using things like templates to automatically generate efficient code tailored for very specific situations. This is also possible in C but not as easy. So C++ may have a performance advantage for the same effort in some situations (but I doubt it's a big advantage or even close to it...).
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: c vs c++

Post by bob »

ethanara wrote:Hi again
I was thinking which language is fastest at chess programming, c or c++ ?
I think it would be possible to make a c++ version of micromax and test result, or making a c version of stockfish
regards
Ethan Ara
If you know what you are doing, there is absolutely no difference. If you get hung up on creating and destroying objects willy-nilly in the search or evaluation, C will win by a wide margin.
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: c vs c++

Post by lucasart »

bob wrote:
ethanara wrote:Hi again
I was thinking which language is fastest at chess programming, c or c++ ?
I think it would be possible to make a c++ version of micromax and test result, or making a c version of stockfish
regards
Ethan Ara
If you know what you are doing, there is absolutely no difference. If you get hung up on creating and destroying objects willy-nilly in the search or evaluation, C will win by a wide margin.
exactly!
the only problem is that the intricacies of C++ have now become so complex that, in fact, very few C++ programmers know what they are doing...
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: c vs c++

Post by lucasart »

ethanara wrote:Hi again
I was thinking which language is fastest at chess programming, c or c++ ?
I think it would be possible to make a c++ version of micromax and test result, or making a c version of stockfish
regards
Ethan Ara
When the programmer understands all of what a C++ compiler actually does, then C++ code is almost as efficient as C code. StockFish is one of the rare examples of perfect C++ code.my general advice is that so long as you dont understand exactly what the C++ compiler does, ie you could write a C equivalent of it, then you shouldn't be writing in C++. So much bad code is being written in C++ by people who have no understanding of what they're doing...
Also, and contrary to popular belief, there nothing C++ can do that C can't do. The whole GNU/Linux operating system is written in C. even GUI programming (the best example being the GTK API and the GNOME desktop).
Last edited by lucasart on Mon Jul 11, 2011 6:39 pm, edited 1 time in total.
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: c vs c++

Post by mcostalba »

lucasart wrote:
bob wrote:
ethanara wrote:Hi again
I was thinking which language is fastest at chess programming, c or c++ ?
I think it would be possible to make a c++ version of micromax and test result, or making a c version of stockfish
regards
Ethan Ara
If you know what you are doing, there is absolutely no difference. If you get hung up on creating and destroying objects willy-nilly in the search or evaluation, C will win by a wide margin.
exactly!
the only problem is that the intricacies of C++ have now become so complex that, in fact, very few C++ programmers know what they are doing...
Regarding to avoid unwanted hidden copies the rules are very simple and I think any average programmer can avoid the pitfalls if he is aware enough of what he is doing when coding and to be aware is really the key word here when programming and even more when programming a chess engine, otherwise you type on your keyboard monkey style and in this (not so uncommon) case no language can come to rescue.

Regarding the language choice, it is actually the opposite: you know one language then you write your chess engine in that language. Wondering if it is better C or C++ suggests that perhaps the first prerequisite (knowing a language) is missing ;-)
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: c vs c++

Post by mcostalba »

lucasart wrote: Also, and contrary to popular belief, there nothing C++ can do that C can't do.
Also, and contrary to popular belief, there nothing C can do that assembly can't do. :-)

This is just a joke Lucas, thanks for your kind words on SF !
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: c vs c++

Post by mcostalba »

BTW, talking about languages, I stumbled across this go language:

http://golang.org/

I have to say that it has some good ideas...it is not bad ! But I have not coded in this language, so my opinion is very very just a guess.
ethanara
Posts: 134
Joined: Mon May 16, 2011 6:58 pm
Location: Denmark

Re: c vs c++

Post by ethanara »

mcostalba wrote:BTW, talking about languages, I stumbled across this go language:

http://golang.org/

I have to say that it has some good ideas...it is not bad ! But I have not coded in this language, so my opinion is very very just a guess.
Interesting. Nearly all things made by google has been succesful, so maybe one will give it a try.
I was thinking if its speed is comparable to c / c++ ?
Also, is there any modern engines made in ASM ?
Regards,
Ethan