Programming language choice, why C\C++ ?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

MahmoudUthman
Posts: 234
Joined: Sat Jan 17, 2015 11:54 pm

Programming language choice, why C\C++ ?

Post by MahmoudUthman »

why are most engines (especially very strong ones ) written in C\C++ , is it due to a performance difference and if so how large is it "given the fact that algorithms are way more important than small optimizations" ?
PK
Posts: 893
Joined: Mon Jan 15, 2007 11:23 am
Location: Warsza

Re: Programming language choice, why C\C++ ?

Post by PK »

Big folks write in C/C++ because of speed. Small folks, including myself - because there is much more code to read and learn from in C/C++ than in any other language.
lauriet
Posts: 199
Joined: Sun Nov 03, 2013 9:32 am

Re: Programming language choice, why C\C++ ?

Post by lauriet »

The language 'should' be irrelevant.
All compilers 'should' produce perfect/fastest code.
You 'should' use the language that you feel most comfortable with.

Now in the real world.............................
Dann Corbit
Posts: 12541
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: Programming language choice, why C\C++ ?

Post by Dann Corbit »

C and C++ are about 1/3 faster to twice as fast compared to CLR type languages or Java.

Most engines are written in C or C++ due to this small speed difference and also due to popularity. If you want to find an example of something it is easy to find in C++ or C and harder to find in other languages.

Fortran, for instance, should produce exactly the same speed quality as C, but Fortran engines are so rare, you won't find many examples of how to do things.
Taking ideas is not a vice, it is a virtue. We have another word for this. It is called learning.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.
jdart
Posts: 4367
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: Programming language choice, why C\C++ ?

Post by jdart »

Speed, and portabilty. But there is a downside. I think it was Bjarne Stoustroup who said, you can shoot off your foot with C. But you can shoot off your whole leg with C++.

--Jon
Ed Trice
Posts: 100
Joined: Fri Sep 19, 2014 5:03 am

Re: Programming language choice, why C\C++ ?

Post by Ed Trice »

jdart wrote:Bjarne Stoustroup who said, you can shoot off your foot with C. But you can shoot off your whole leg with C++.
A funny quote.

But to be more apt, you can shoot off your foot in C, plus leave a huge hole in the earth under your foot, with one line of code.

You can Whole.Leg.Yours->shoot.off in C++, but it would take an entire development team one man-week to figure out whose leg is missing :)
brtzsnr
Posts: 433
Joined: Fri Jan 16, 2015 4:02 pm

Re: Programming language choice, why C\C++ ?

Post by brtzsnr »

I chose Go for readability and development speed. I managed to build a whole suite of tools around the engine (testing framework, tuner, analyzer, epd solver, etc) with very little effort and very few external libraries.

The compiler is getting better every the release, and the gap of the generated code quality is small enough. I even helped improve the compiler so that zurichess doesn't suck. Currently it's missing the popcnt intrinsic, but it's going to be added in Go1.9 after I proposed the math/bits library [1].

I'm a mainly C++ developer at work (with some Python & Go in between), but I would stlll not choose C++ for any of my personal projects. It's very verbose compared to Go and the public libraries are either very verbose or very complex.

[1] https://github.com/golang/go/issues/18616
Karlo Bala
Posts: 373
Joined: Wed Mar 22, 2006 10:17 am
Location: Novi Sad, Serbia
Full name: Karlo Balla

Re: Programming language choice, why C\C++ ?

Post by Karlo Bala »

MahmoudUthman wrote:why are most engines (especially very strong ones ) written in C\C++ , is it due to a performance difference and if so how large is it "given the fact that algorithms are way more important than small optimizations" ?
From the very beginning, C was a small, simple and close to the hardware language. It is also character economic language, without a lot of "begin" and "end" type keywords. One can learn C basics in less than a week. C++ is more or less the super-set of C, and a lot of programmers just use a C++ compiler to write enhanced C code.

On the other hand, for languages like C# you really need to know what you are doing to write an efficient code. The standard is still changing and without much experience, in programs for crunching numbers (like chess) there is a big chance to end up burning 90% CPU cycles on creating objects and collecting garbage.
Best Regards,
Karlo Balla Jr.
AlvaroBegue
Posts: 931
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: Programming language choice, why C\C++ ?

Post by AlvaroBegue »

Karlo Bala wrote:
MahmoudUthman wrote:why are most engines (especially very strong ones ) written in C\C++ , is it due to a performance difference and if so how large is it "given the fact that algorithms are way more important than small optimizations" ?
From the very beginning, C was a small, simple and close to the hardware language. It is also character economic language, without a lot of "begin" and "end" type keywords. One can learn C basics in less than a week. C++ is more or less the super-set of C, and a lot of programmers just use a C++ compiler to write enhanced C code.

On the other hand, for languages like C# you really need to know what you are doing to write an efficient code. The standard is still changing and without much experience, in programs for crunching numbers (like chess) there is a big chance to end up burning 90% CPU cycles on creating objects and collecting garbage.
That's exactly the way I see it. Notice that if you are not careful with C++, you can still spend the majority of your time allocating memory, initializing it and deallocating it.

C and C++ allow you to waste very little CPU in anything that is not the computation you are trying to do, but a beginner might have a hard time taking advantage of this. For instance, if your move generator returns a std::list<Move>, I expect your engine will be very slow.
abulmo2
Posts: 433
Joined: Fri Dec 16, 2016 11:04 am
Location: France
Full name: Richard Delorme

Re: Programming language choice, why C\C++ ?

Post by abulmo2 »

jdart wrote:Speed, and portabilty.
Speed? Why not D, fortran, pascal, java, ada, go, rust? They are similar in speed to C or C++ (they all have a gcc or llvm backend). And if you really want speed asmfish is a convincing proof that the only solution is an assembly language.

Portability? Who cares? If it runs under Windows/intel, it's enough (Houdini for example runs only under windows).

When I developed amoeba using the D language, I was very surprised that no chess engine has ever been written in D. I am still wondering why people stick to C or C++.
Richard Delorme