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

rbarreira
Posts: 900
Joined: Tue Apr 27, 2010 3:48 pm

Re: c vs c++

Post by rbarreira »

SuneF wrote: If you stick to just ansi C it is fairly portable but still in need of special native assemblies as opposed to eg Java. Once you begin with threads or anything requirering 3rd party libs it gets messy.
Java which doesn't even run on all mobile platforms (last I checked, due to licensing reasons), is that what you call a portable language?
In C# forinstance you can do:

var numbers = new List<int> { 1,4,5,6,2,1,3,54,6};
numbers.Add(36);
numbers.Sort();

With just those three simple lines you get qsorting and you get a resizable int array. Try writing the C-equivalent of that, it will be huge
It wouldn't be huge at all if you use a linked list library. I didn't say C has more native libraries, but it does have a huge amount of good libraries that you can easily import.
You need to know how pointers, heaps and stacks work sure. You don't need to learn C for that though.
Maybe you don't need to, but it sure looks to me like many programmers never learn it if they are in the comfortable zone of highly abstracted languages.
wgarvin
Posts: 838
Joined: Thu Jul 05, 2007 5:03 pm
Location: British Columbia, Canada

Re: c vs c++

Post by wgarvin »

mcostalba wrote: 3) std::max_element() is a function, is not inlined and so there is some overhead there.
Are you sure about that? It's a templated inline function in a header file...

Here's the version from MSVC 2008 for instance (I fixed the indenting though):

Code: Select all

template<class _FwdIt> inline
_FwdIt _Max_element&#40;_FwdIt _First, _FwdIt _Last&#41;
&#123;
    // find largest element, using operator<
    _DEBUG_RANGE&#40;_First, _Last&#41;;
    _FwdIt _Found = _First;
    if (_First != _Last&#41;
        for (; ++_First != _Last; )
            if (_DEBUG_LT&#40;*_Found, *_First&#41;)
                _Found = _First;
    return (_Found&#41;;
&#125;

template<class _FwdIt> inline
_FwdIt max_element&#40;_FwdIt _First, _FwdIt _Last&#41;
&#123;
    // find largest element, using operator<
    _ASSIGN_FROM_BASE&#40;_First,
        _Max_element&#40;_CHECKED_BASE&#40;_First&#41;, _CHECKED_BASE&#40;_Last&#41;));
    return (_First&#41;;
&#125;
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: c vs c++

Post by mcostalba »

wgarvin wrote: Are you sure about that? It's a templated inline function in a header file...
I am using mingw gcc, I will check better though.

Anyhow this is a fast speed test on my slow 32bit Core Duo T5250

./stockfish bench > /dev/null


SF dev version: 6836674 nodes searched, 408232 nodes/sec

Rein version: 6696967 nodes searched, 403504 nodes/sec
SuneF
Posts: 127
Joined: Thu Sep 17, 2009 11:19 am

Re: c vs c++

Post by SuneF »

rbarreira wrote:
SuneF wrote: If you stick to just ansi C it is fairly portable but still in need of special native assemblies as opposed to eg Java. Once you begin with threads or anything requirering 3rd party libs it gets messy.
Java which doesn't even run on all mobile platforms (last I checked, due to licensing reasons), is that what you call a portable language?
Yes Java is extremly portable, that was also one of its key design goals.
Nothing is portable to ALL platforms, so not sure what your point is.
In C# forinstance you can do:

var numbers = new List<int> { 1,4,5,6,2,1,3,54,6};
numbers.Add(36);
numbers.Sort();

With just those three simple lines you get qsorting and you get a resizable int array. Try writing the C-equivalent of that, it will be huge
It wouldn't be huge at all if you use a linked list library. I didn't say C has more native libraries, but it does have a huge amount of good libraries that you can easily import.
The List<T> is like I says a dynamic array, not a linked list. You can apply the index operator which you can't do on a linked list.
And yes you probably can find libraries here or there of arguably questionable value which offers this. I personally think it should be an integral part of the language itself, we all need it all the time anyway so why waste time downloading and installing 3rd party libs for the most common things.
You need to know how pointers, heaps and stacks work sure. You don't need to learn C for that though.
Maybe you don't need to, but it sure looks to me like many programmers never learn it if they are in the comfortable zone of highly abstracted languages.
You might be right. But I also see a lot of C/C++ programmers who are stuck in the 90ties with their microoptimizations and doesn't realize how much software and programming has evolved during the last few decades. ;-)
SuneF
Posts: 127
Joined: Thu Sep 17, 2009 11:19 am

Re: c vs c++

Post by SuneF »

Desperado wrote:
rbarreira wrote:
SuneF wrote: If you're interested in becomming a proficient versatile programmer then forget C/C++, learn a modern more powerful language. You'll build the engine faster, it'll have fewer bugs, it will be easier to deploy on different platforms and you'll have learned a language you can actually use for developing other applications.
So what is this language which allows you to make a chess engine faster, with fewer bugs, more portable and more widely used ... ?
i think it s called " CopyAndPaste+ " :)
Haha well not quite what I had in mind :)

I was thinking C# or Java actually. I've been toying with the idea of starting a small compact engine in C#, just to see how compact I could code it using all the tricks available and just give a damn about the speed.
Maybe a multivariant sort of engine, never done one of those before.
rbarreira
Posts: 900
Joined: Tue Apr 27, 2010 3:48 pm

Re: c vs c++

Post by rbarreira »

SuneF wrote:
rbarreira wrote:
SuneF wrote: If you stick to just ansi C it is fairly portable but still in need of special native assemblies as opposed to eg Java. Once you begin with threads or anything requirering 3rd party libs it gets messy.
Java which doesn't even run on all mobile platforms (last I checked, due to licensing reasons), is that what you call a portable language?
Yes Java is extremly portable, that was also one of its key design goals.
Nothing is portable to ALL platforms, so not sure what your point is.
My point is that Java is not as portable as C/C++, that simple. If you have a Java program and want to get it running across several kinds of mobile phones (a quite important platform these days), you're out of luck.
Rein Halbersma
Posts: 741
Joined: Tue May 22, 2007 11:13 am

Re: c vs c++

Post by Rein Halbersma »

mcostalba wrote:
Rein Halbersma wrote: That is because your current algorithm is not stable and mine is :-)
Hi Rein,

yes you are right !

This makes to compare speeds a bit more difficult because searched nodes are little different. Under this non-optimal conditions current SF code _seems_ a bit faster of your fastest version, that is the "always swap" one.

It is not self evident to understand why, we have to consider the surrounding conditions in which that code is used:

1) It is manly used only for captures that are very few, something from 0 to 4-5 and the code actually kicks in when we have at least 2 captures.

2) The swap is done with a register and very well optimized by the compiler

3) std::max_element() is a function, is not inlined and so there is some overhead there.

4) Pointers are a little slower than values, for instance I have tried to change current SF version using Move* bestMovePtr instead of Move bestMove and results are slightly slower.

So overall I think your idea is good, but possibly it needs further tuning work.

Marco

P.S: std::swap() uses some ad-hoc gcc magic (_GLIBCXX_MOVE) that probably is faster then the usual tmp way. On MSVC instead the swap is implemented with the usual algorithm you can find in current SF.
Hi Marco,

I always find it more useful to get a correct program first and then worry about efficiency.

The current loop has a subtle side effect, it is not only unstable, but it also loses the best move picked from the move list and instead duplicates the first move. So pick_best is not even a partial unstable sort! Your current search might not depend on move order stability or the fact that you lose information, and for now your program might be correct. But someday a rewrite might occur that violates this hidden assumption. E.g. if you ever decide to write out iterative deepening as iteration instead of recursion, you cannot re-use the generated move list because pick_best_move dissipates information. It seems simply not worth the trouble.

Insisting on manually inlining std::max_element and/or std::swap doesn't make sense to me either. It is something the compiler should do for you, and the STL is source code templates (here already an advantage over C library!), so the compiler can do so if necessary. And if it doesn't do it even after pgo runs, then it simply doesn't matter anyway. But inlining would be at least an improvement on doing multiple swaps and overwriting the first entry of the move list.

The proposal reduces the pick_best from 13 lines with subtle side effects to 3 lines that guarantee a stable partial sort. It's simply more maintainable and readable. I am confident the change would not cost even a single ELO point (if anything, the reduced number of swaps should be a gain), but even if it did, then the simplicity alone would be worth that tiny cost.

Rein

P.S. in the current Stockfish implementation, there is still one micro-optimization left by writing T bestMove = *curMove (i.e. combine declaration and the assignment into a single initalization, but perhaps the compiler already generated this for you)
Last edited by Rein Halbersma on Tue Jul 12, 2011 3:11 pm, edited 1 time in total.
Rein Halbersma
Posts: 741
Joined: Tue May 22, 2007 11:13 am

Re: c vs c++

Post by Rein Halbersma »

mcostalba wrote:
wgarvin wrote: Are you sure about that? It's a templated inline function in a header file...
I am using mingw gcc, I will check better though.

Anyhow this is a fast speed test on my slow 32bit Core Duo T5250

./stockfish bench > /dev/null


SF dev version: 6836674 nodes searched, 408232 nodes/sec

Rein version: 6696967 nodes searched, 403504 nodes/sec
So times to depth are SF=16.747 sec vs and proposal=16.597 sec (1% gain)
wgarvin
Posts: 838
Joined: Thu Jul 05, 2007 5:03 pm
Location: British Columbia, Canada

Re: c vs c++

Post by wgarvin »

mcostalba wrote:
wgarvin wrote: Are you sure about that? It's a templated inline function in a header file...
I am using mingw gcc, I will check better though.

Anyhow this is a fast speed test on my slow 32bit Core Duo T5250

./stockfish bench > /dev/null


SF dev version: 6836674 nodes searched, 408232 nodes/sec

Rein version: 6696967 nodes searched, 403504 nodes/sec
If your version is faster, maybe it doesn't really matter much why. std::max_element might be handy when trying to do some high-level algorithmic thing, but for what you're doing who cares if its 1 line of code or your hand-rolled 3 lines of code? Rather than trust that every compiler's implementation of std::max_element is efficient for your use case, you should just keep your code the way it is. :D

But if you're interested in figuring it out... maybe you could put those two snippets in their own source file, and generate an assembly listing and post it here? Maybe there will be something obvious about the code generated.
SuneF
Posts: 127
Joined: Thu Sep 17, 2009 11:19 am

Re: c vs c++

Post by SuneF »

rbarreira wrote:
SuneF wrote:
rbarreira wrote:
SuneF wrote: If you stick to just ansi C it is fairly portable but still in need of special native assemblies as opposed to eg Java. Once you begin with threads or anything requirering 3rd party libs it gets messy.
Java which doesn't even run on all mobile platforms (last I checked, due to licensing reasons), is that what you call a portable language?
Yes Java is extremly portable, that was also one of its key design goals.
Nothing is portable to ALL platforms, so not sure what your point is.
My point is that Java is not as portable as C/C++, that simple. If you have a Java program and want to get it running across several kinds of mobile phones (a quite important platform these days), you're out of luck.
You must be referring to Apple platform? If so you are out of luck with C too, it's call Objective-C and is something different entirely. Of course if you jail-break it works. It's more of a business decision than a limitation of the Java language. Apple wants to fight everyone for some reason.

Windows Mobile and Android supports Java just fine of course, Android _is_ java.