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

User avatar
edlich
Posts: 8
Joined: Wed Jul 13, 2011 12:00 pm
Location: Berlin

Re: c vs c++

Post by edlich »

rbarreira wrote: Compiler quality, portability, API maturity, performance and famliarity/popularity are all advantages of C/C++ over D.
Thanks a lot for your insights. I really do appreciate them. My comments:

- Compiler quality
Accepted: MS C++ / gcc is outstanding. Nevertheless tell me another guy with more experience in C++ (! and D) Compilers then Walter Bright.

- portability
Your point. But I like to ask if this is important for chess programmers. Perhaps I have limited knowledge here.

- API maturity
Not sure if this is an advantage. It's the same discussion with the Java API. Is it good if an API is fixed for 20 years but not extending (C# extended great compared to Java). The D core is stable since 5-10 years. Extensions can be used or ignored.

- performance
If the performance between C and C++ is unclear (also because of the kind of usage) I assume the performance between C++ and D is also unclear. Would you take a bet that a TSCP port to D is more then 5 or 10 % slower? (Take care, it might be faster ;-)

- and famliarity/popularity
This point goes to you. No objections here. The book amount is surely over 1:100 better for C++.

Now it's up to our developers to decide if these advantages outweight the points I listed on the chessprogrammin wiki page for D. I hope we are doing them a favour.

Best & thanks
Stefan Edlich
User avatar
michiguel
Posts: 6401
Joined: Thu Mar 09, 2006 8:30 pm
Location: Chicago, Illinois, USA

Re: c vs c++

Post by michiguel »

SuneF wrote:
Don wrote:
SuneF wrote:
ethanara wrote:I was talking 100% about exe performance
I did read in a java book that in some tasks java would be faster than c++
If you will i give you example and execution time.
Do you guys think java would be faster than c c++ in a chess engine?
Or would it be faster to make a multi-language like c java chess engine?
Bye
Ethan Ara
Java is JIT compiled, meaning the java machine will delay the translation of code till runtime. This is what makes java so portable and in principle it could be faster than any C program.
The phrase "in principle" is misleading because in practice it will never be faster than C given equally good compiler implementations.

In principle C could do all of the thingsJIT does with PGO.
I'm not so sure. With late binding at runtime you would know the instruction set of the CPU, whether it was AMD, Intel, PIV, Core2, 64-bit, number of cores, SSE and so on. A smart (really smart) java machine would be able to optimize for the specific platform. The PGO might also perform differently on AMD vs Intel, or small caches vs large caches etc.. so many variables.
In principle every language could anticipate the programmers intent and even use different algorithms. (I wanted bubble sort, but the compiler detects that I have implemented a bubble sort and substitutes a quicksort.)

But in practice Java is a big problem for chess programs, primarily due to memory issues (memory management and lack of fine control over memory layout.)

Maybe in a more distant future these problems will be solved with Java, but then it has become a new language and it's already showing signs of age (as is C and C++) so it's probably a moot point.

I tihnk it's a safe bet to say that we will never see a top 10 program written in Java. If it happens it will not be because it was written in Java but because the programmer wrote a far superior program (and would have done much better in C.)
It probably stands to lose a few hundred Elo given the same program written in Java, but then the point is you wouldn't have the same program in Java. The 50% time saved hunting bugs is 50% more time to develop new algorithms and tricks in java.
IMHO, nasty bugs in an engine are not language related.

Miguel
SuneF
Posts: 127
Joined: Thu Sep 17, 2009 11:19 am

Re: c vs c++

Post by SuneF »

Daniel Shawul wrote:I like java too even though I started using it only past couple of months.
It has a lot of libraries that could have saved me a lot of development time.
C is not even the best high level language for number crunching. FORTRAN is better , faster and has many libraries c could never have.
Yes Fortran is _the_ languange for number crunching. In C I've always wondered why there wasn't a foreach loop, why do I need to waste a register to declare an index variable, then test it and increment it and finally index every element in the array when I don't really need to? Frankly it's idiotic and cumbersome notation.

I guess at the end of the day it's mostly down to the compilers. However C doesn't make it easy with all the pointers flying around, certain assumptions are difficult for the compiler to make.
I don't see too much of a difference in c and c++ for chess applications, because you would not use many features c++ anyway.
A chess engine is not the best application to learn an OOP language.
At certain hotspots we have to avoid it, but in other some places we can make use of it. The code might be cleaner if we did full inheritage of the pieces, but then still it's their relationships that are important, like a queen threatening a king.
Janzert

Re: c vs c++

Post by Janzert »

edlich wrote:Thus it would be interesting to know why still so many chess developers decide to to go for C++. Is it a mass effect? Is it the amount of available (clonable? ;-) chess code? Or something more I miss? (and keen to learn!).
Not quite chess but certainly using all the same techniques, I wrote an arimaa engine in D (OpFor). I retired it this year to start over with C++ primarily because I was tired of dealing with compiler bugs. While I'm sure the D compilers are probably plenty stable for general code, once it gets down to the (otherwise obscure) bit twiddling that is fairly common in chess or arimaa bitboard engine code running into compiler bugs became a real pain for me.

No stable 64bit compiler and the seemingly constant language evolution were also large negatives for me.

Janzert
SuneF
Posts: 127
Joined: Thu Sep 17, 2009 11:19 am

Re: c vs c++

Post by SuneF »

michiguel wrote:
SuneF wrote:
Don wrote:
SuneF wrote:
ethanara wrote:I was talking 100% about exe performance
I did read in a java book that in some tasks java would be faster than c++
If you will i give you example and execution time.
Do you guys think java would be faster than c c++ in a chess engine?
Or would it be faster to make a multi-language like c java chess engine?
Bye
Ethan Ara
Java is JIT compiled, meaning the java machine will delay the translation of code till runtime. This is what makes java so portable and in principle it could be faster than any C program.
The phrase "in principle" is misleading because in practice it will never be faster than C given equally good compiler implementations.

In principle C could do all of the thingsJIT does with PGO.
I'm not so sure. With late binding at runtime you would know the instruction set of the CPU, whether it was AMD, Intel, PIV, Core2, 64-bit, number of cores, SSE and so on. A smart (really smart) java machine would be able to optimize for the specific platform. The PGO might also perform differently on AMD vs Intel, or small caches vs large caches etc.. so many variables.
In principle every language could anticipate the programmers intent and even use different algorithms. (I wanted bubble sort, but the compiler detects that I have implemented a bubble sort and substitutes a quicksort.)

But in practice Java is a big problem for chess programs, primarily due to memory issues (memory management and lack of fine control over memory layout.)

Maybe in a more distant future these problems will be solved with Java, but then it has become a new language and it's already showing signs of age (as is C and C++) so it's probably a moot point.

I tihnk it's a safe bet to say that we will never see a top 10 program written in Java. If it happens it will not be because it was written in Java but because the programmer wrote a far superior program (and would have done much better in C.)
It probably stands to lose a few hundred Elo given the same program written in Java, but then the point is you wouldn't have the same program in Java. The 50% time saved hunting bugs is 50% more time to develop new algorithms and tricks in java.
IMHO, nasty bugs in an engine are not language related.

Miguel
Well... Ever had a buffer overlow bug where you given certain conditions would overwrite some other vital structure? Notoriously hard to debug, especially if the condition causing it is rare and doesn't reproduce. In many modern languages this cannot happen. And if something bad does happen, you at least get a useful exception that tells you where and what happenend.
ethanara
Posts: 134
Joined: Mon May 16, 2011 6:58 pm
Location: Denmark

Re: c vs c++

Post by ethanara »

Have some of you guys manually asm optimized your program?
If so , can you give some classic examples?
rbarreira
Posts: 900
Joined: Tue Apr 27, 2010 3:48 pm

Re: c vs c++

Post by rbarreira »

edlich wrote:
rbarreira wrote: Compiler quality, portability, API maturity, performance and famliarity/popularity are all advantages of C/C++ over D.
Thanks a lot for your insights. I really do appreciate them. My comments:

- Compiler quality
Accepted: MS C++ / gcc is outstanding. Nevertheless tell me another guy with more experience in C++ (! and D) Compilers then Walter Bright.

- portability
Your point. But I like to ask if this is important for chess programmers. Perhaps I have limited knowledge here.

- API maturity
Not sure if this is an advantage. It's the same discussion with the Java API. Is it good if an API is fixed for 20 years but not extending (C# extended great compared to Java). The D core is stable since 5-10 years. Extensions can be used or ignored.

- performance
If the performance between C and C++ is unclear (also because of the kind of usage) I assume the performance between C++ and D is also unclear. Would you take a bet that a TSCP port to D is more then 5 or 10 % slower? (Take care, it might be faster ;-)

- and famliarity/popularity
This point goes to you. No objections here. The book amount is surely over 1:100 better for C++.

Now it's up to our developers to decide if these advantages outweight the points I listed on the chessprogrammin wiki page for D. I hope we are doing them a favour.

Best & thanks
Stefan Edlich
Compiler quality - I am talking about the present situation. I am not making a statement about the technical competence of the inventors of D.

Portability - I think it is important for some chess authors yes. For example, I believe right now there's still no 64-bit D compiler for Windows (or is it Linux? I know one of them got a 64-bit compiler recently, not sure which). Then there are people who want to port their chess engines to mobile phones and such, for those people portability is quite important.

API maturity - I heard there are two incompatible competing APIs for D - Tango and Phobos. I don't know the details but I did hear that they've broken backwards compatibility with some releases. But this is hearsay, someone correct me if that's wrong.

Performance - Now that D has a 64-bit compiler for some platforms, the difference might not be as big as it was a while ago, but I would still bet that you'd see much more than a 10% performance difference vs the best C compilers (like gcc 4.5 and the Intel C compiler).
rbarreira
Posts: 900
Joined: Tue Apr 27, 2010 3:48 pm

Re: c vs c++

Post by rbarreira »

Janzert wrote:
edlich wrote:Thus it would be interesting to know why still so many chess developers decide to to go for C++. Is it a mass effect? Is it the amount of available (clonable? ;-) chess code? Or something more I miss? (and keen to learn!).
Not quite chess but certainly using all the same techniques, I wrote an arimaa engine in D (OpFor). I retired it this year to start over with C++ primarily because I was tired of dealing with compiler bugs. While I'm sure the D compilers are probably plenty stable for general code, once it gets down to the (otherwise obscure) bit twiddling that is fairly common in chess or arimaa bitboard engine code running into compiler bugs became a real pain for me.

No stable 64bit compiler and the seemingly constant language evolution were also large negatives for me.

Janzert
Oh hi, there you are, I thought about you a few times throughout this thread and may even have mentioned you (not by name :)).
User avatar
michiguel
Posts: 6401
Joined: Thu Mar 09, 2006 8:30 pm
Location: Chicago, Illinois, USA

Re: c vs c++

Post by michiguel »

SuneF wrote:
michiguel wrote:
SuneF wrote:
Don wrote:
SuneF wrote:
ethanara wrote:I was talking 100% about exe performance
I did read in a java book that in some tasks java would be faster than c++
If you will i give you example and execution time.
Do you guys think java would be faster than c c++ in a chess engine?
Or would it be faster to make a multi-language like c java chess engine?
Bye
Ethan Ara
Java is JIT compiled, meaning the java machine will delay the translation of code till runtime. This is what makes java so portable and in principle it could be faster than any C program.
The phrase "in principle" is misleading because in practice it will never be faster than C given equally good compiler implementations.

In principle C could do all of the thingsJIT does with PGO.
I'm not so sure. With late binding at runtime you would know the instruction set of the CPU, whether it was AMD, Intel, PIV, Core2, 64-bit, number of cores, SSE and so on. A smart (really smart) java machine would be able to optimize for the specific platform. The PGO might also perform differently on AMD vs Intel, or small caches vs large caches etc.. so many variables.
In principle every language could anticipate the programmers intent and even use different algorithms. (I wanted bubble sort, but the compiler detects that I have implemented a bubble sort and substitutes a quicksort.)

But in practice Java is a big problem for chess programs, primarily due to memory issues (memory management and lack of fine control over memory layout.)

Maybe in a more distant future these problems will be solved with Java, but then it has become a new language and it's already showing signs of age (as is C and C++) so it's probably a moot point.

I tihnk it's a safe bet to say that we will never see a top 10 program written in Java. If it happens it will not be because it was written in Java but because the programmer wrote a far superior program (and would have done much better in C.)
It probably stands to lose a few hundred Elo given the same program written in Java, but then the point is you wouldn't have the same program in Java. The 50% time saved hunting bugs is 50% more time to develop new algorithms and tricks in java.
IMHO, nasty bugs in an engine are not language related.

Miguel
Well... Ever had a buffer overlow bug where you given certain conditions would overwrite some other vital structure? Notoriously hard to debug, especially if the condition causing it is rare and doesn't reproduce.
Yes, but those were never the most difficult bugs to debug. There are not many buffers like that in a chess engine (compared to other programs), and there are many techniques that make this not to problematic. Proper use of asserts, tests etc. help a lot. In fact, I caught some of those before even showed up.

Bugs that cost me time were generally chess related, hashtable related, smp related, or wrong implementations of a given algorithm. If figure I would have those in any language.

In many modern languages this cannot happen. And if something bad does happen, you at least get a useful exception that tells you where and what happenend.
Yes, that is useful, but not critical in a chess engine, IMHO. What I would say other languages would help in speeding up development is in writing tools needed for engine building. But you can write them in other languages anyway.

Miguel
User avatar
michiguel
Posts: 6401
Joined: Thu Mar 09, 2006 8:30 pm
Location: Chicago, Illinois, USA

Re: c vs c++

Post by michiguel »

rbarreira wrote:
edlich wrote:
rbarreira wrote: Compiler quality, portability, API maturity, performance and famliarity/popularity are all advantages of C/C++ over D.
Thanks a lot for your insights. I really do appreciate them. My comments:

- Compiler quality
Accepted: MS C++ / gcc is outstanding. Nevertheless tell me another guy with more experience in C++ (! and D) Compilers then Walter Bright.

- portability
Your point. But I like to ask if this is important for chess programmers. Perhaps I have limited knowledge here.

- API maturity
Not sure if this is an advantage. It's the same discussion with the Java API. Is it good if an API is fixed for 20 years but not extending (C# extended great compared to Java). The D core is stable since 5-10 years. Extensions can be used or ignored.

- performance
If the performance between C and C++ is unclear (also because of the kind of usage) I assume the performance between C++ and D is also unclear. Would you take a bet that a TSCP port to D is more then 5 or 10 % slower? (Take care, it might be faster ;-)

- and famliarity/popularity
This point goes to you. No objections here. The book amount is surely over 1:100 better for C++.

Now it's up to our developers to decide if these advantages outweight the points I listed on the chessprogrammin wiki page for D. I hope we are doing them a favour.

Best & thanks
Stefan Edlich
Compiler quality - I am talking about the present situation. I am not making a statement about the technical competence of the inventors of D.

Portability - I think it is important for some chess authors yes. For example, I believe right now there's still no 64-bit D compiler for Windows (or is it Linux? I know one of them got a 64-bit compiler recently, not sure which). Then there are people who want to port their chess engines to mobile phones and such, for those people portability is quite important.
Is there any system we cannot find a C compiler for it?

Miguel
API maturity - I heard there are two incompatible competing APIs for D - Tango and Phobos. I don't know the details but I did hear that they've broken backwards compatibility with some releases. But this is hearsay, someone correct me if that's wrong.

Performance - Now that D has a 64-bit compiler for some platforms, the difference might not be as big as it was a while ago, but I would still bet that you'd see much more than a 10% performance difference vs the best C compilers (like gcc 4.5 and the Intel C compiler).