c vs c++

Discussion of chess software programming and technical issues.

Moderators: hgm, Harvey Williamson, bob

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.

c or c++ ?

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

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

c vs c++

Post by ethanara » Sun Jul 10, 2011 7:52 pm

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: 2909
Joined: Fri Jan 21, 2011 11:42 pm
Location: NL
Contact:

Re: c vs c++

Post by Evert » Sun Jul 10, 2011 8:20 pm

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 1:48 pm

Re: c vs c++

Post by rbarreira » Sun Jul 10, 2011 8:52 pm

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: 20358
Joined: Mon Feb 27, 2006 6:30 pm
Location: Birmingham, AL

Re: c vs c++

Post by bob » Sun Jul 10, 2011 9:43 pm

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: 3029
Joined: Mon May 31, 2010 11:29 am
Full name: lucasart
Contact:

Re: c vs c++

Post by lucasart » Mon Jul 11, 2011 4:15 pm

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: 3029
Joined: Mon May 31, 2010 11:29 am
Full name: lucasart
Contact:

Re: c vs c++

Post by lucasart » Mon Jul 11, 2011 4:38 pm

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 4:39 pm, edited 1 time in total.

mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 7:17 pm

Re: c vs c++

Post by mcostalba » Mon Jul 11, 2011 4:39 pm

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 7:17 pm

Re: c vs c++

Post by mcostalba » Mon Jul 11, 2011 4:43 pm

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 7:17 pm

Re: c vs c++

Post by mcostalba » Mon Jul 11, 2011 5:04 pm

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 4:58 pm
Location: Denmark
Contact:

Re: c vs c++

Post by ethanara » Mon Jul 11, 2011 6:19 pm

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

Post Reply