Computer languages engines implemented with

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

kadr

Computer languages engines implemented with

Post by kadr »

I am implementing a prototype of my chess engine with C#. I think I cannot achieve the same or at least comparable performance as for good engines with .Net. I use it only because it is more convenient personally for me. I am going to rewrite the engine with c++ ( not managed c++.Net). Many opensource engines are implemented with just C. I am interested in any thoughts and suggestions about the topic.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Computer languages engines implemented with

Post by bob »

kadr wrote:I am implementing a prototype of my chess engine with C#. I think I cannot achieve the same or at least comparable performance as for good engines with .Net. I use it only because it is more convenient personally for me. I am going to rewrite the engine with c++ ( not managed c++.Net). Many opensource engines are implemented with just C. I am interested in any thoughts and suggestions about the topic.
C++ will work fine but you want to avoid the typical object-oriented approach of creating objects and destroying them right and left. You don't want to create a new "board object" for example, every time you make a move. If you program reasonably, there is no difference in speed between C and C++...
kadr

Re: Computer languages engines implemented with

Post by kadr »

bob wrote:
kadr wrote:I am implementing a prototype of my chess engine with C#. I think I cannot achieve the same or at least comparable performance as for good engines with .Net. I use it only because it is more convenient personally for me. I am going to rewrite the engine with c++ ( not managed c++.Net). Many opensource engines are implemented with just C. I am interested in any thoughts and suggestions about the topic.
C++ will work fine but you want to avoid the typical object-oriented approach of creating objects and destroying them right and left. You don't want to create a new "board object" for example, every time you make a move. If you program reasonably, there is no difference in speed between C and C++...
Thanks, Robert.
I am aware that I should avoid allocating memory during search.
But what about objects themselves? If I utilize Board object and its methods instead of global structures and functions I will have additional parameter in methods (object itself) and maybe more sophisticated memory instructions because of the object. Do it influence performance significantly?
diep
Posts: 1822
Joined: Thu Mar 09, 2006 11:54 pm
Location: The Netherlands

Re: Computer languages engines implemented with

Post by diep »

kadr wrote:
bob wrote:
kadr wrote:I am implementing a prototype of my chess engine with C#. I think I cannot achieve the same or at least comparable performance as for good engines with .Net. I use it only because it is more convenient personally for me. I am going to rewrite the engine with c++ ( not managed c++.Net). Many opensource engines are implemented with just C. I am interested in any thoughts and suggestions about the topic.
C++ will work fine but you want to avoid the typical object-oriented approach of creating objects and destroying them right and left. You don't want to create a new "board object" for example, every time you make a move. If you program reasonably, there is no difference in speed between C and C++...
Thanks, Robert.
I am aware that I should avoid allocating memory during search.
But what about objects themselves? If I utilize Board object and its methods instead of global structures and functions I will have additional parameter in methods (object itself) and maybe more sophisticated memory instructions because of the object. Do it influence performance significantly?
It's not so easy to make a C++ engine of the same speed like a C engine if you program object oriented code for your daily work.

It's true that on paper defining the entire C engine as 'extern c++' should on paper be the same speed, but that's just theory.

Practice is that people make classes, subclasses and so on.

In short soon you get in situations where the compiler decides to allocate and implicitly allocate and delete objects.

C++ practical is about 3 times slower than C for most programmers, except those who really know what they're doing AND regurarly print their C++ code to assembler, to see which mess the compiler created.

C# is way worse here, not speaking even yet about managed code nor code with implicit boundschecking (which C# has).

However for the first 2400 elo points (standard) the quality of the program is more interesting than its speed.

Yet speed matters initially, because usually initially it is very difficult to get near the same speed of others.

I remember how i set up my first evaluation function in diep. I used all kind of indirect arrays, in order to be able to automatically tune it. That was januari 1994.

The next years i discovered this was awful slow and replaced the code with variables. Another few years later i replaced the variables with defines whereever possible.

One of my first C++ incarnations was around 1991-1992. An Athletics program. Back then C++ seemed ok. There wasn't idiocy like templates in the books i had about C++ at the time. It did have inheritance and it have some nice things good for companies to shield your code from other parts.

Back then C++ seemed promising.

Now some years later already, C++ got total raped to worlds most complex language, by some authors who kept adding new 'features' to the language.

As a result now every student who tries to write C++ code total messes up, as without exception they try to use the 'cool features that the language has to offer' , instead of using the minimum needed.

If you can resist those temptations and just use a template when it really helps, then C++ is a great language, in all other cases it's doomed to be factors slower than C.

Vincent
Aleks Peshkov
Posts: 892
Joined: Sun Nov 19, 2006 9:16 pm
Location: Russia

Re: Computer languages engines implemented with

Post by Aleks Peshkov »

kadr wrote:I am implementing a prototype of my chess engine with C#. I think I cannot achieve the same or at least comparable performance as for good engines with .Net. I use it only because it is more convenient personally for me. I am going to rewrite the engine with c++ ( not managed c++.Net). Many opensource engines are implemented with just C. I am interested in any thoughts and suggestions about the topic.
Clean programming style and deep understanding of the problem do matter, not a language choice. Real Programmer can write Fortran programs in any language. Try to be a good one...
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Computer languages engines implemented with

Post by bob »

kadr wrote:
bob wrote:
kadr wrote:I am implementing a prototype of my chess engine with C#. I think I cannot achieve the same or at least comparable performance as for good engines with .Net. I use it only because it is more convenient personally for me. I am going to rewrite the engine with c++ ( not managed c++.Net). Many opensource engines are implemented with just C. I am interested in any thoughts and suggestions about the topic.
C++ will work fine but you want to avoid the typical object-oriented approach of creating objects and destroying them right and left. You don't want to create a new "board object" for example, every time you make a move. If you program reasonably, there is no difference in speed between C and C++...
Thanks, Robert.
I am aware that I should avoid allocating memory during search.
But what about objects themselves? If I utilize Board object and its methods instead of global structures and functions I will have additional parameter in methods (object itself) and maybe more sophisticated memory instructions because of the object. Do it influence performance significantly?
I have seen some nicely written programs that use a static board object. You still get to do things like board.MakeMove() and such, but there is only one instance of "board". You can write code just as efficient as what you could produce in C. Of course, you can also write horribly slow code if you get carried away with object-oriented stuff.
jwes
Posts: 778
Joined: Sat Jul 01, 2006 7:11 am

Re: Computer languages engines implemented with

Post by jwes »

Another way of saying this is that it is easier to unknowingly write slow code in c++.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Computer languages engines implemented with

Post by bob »

jwes wrote:Another way of saying this is that it is easier to unknowingly write slow code in c++.
Yes, although it is also possible to unknowingly write slow code in C as well.
mathmoi
Posts: 287
Joined: Mon Mar 13, 2006 5:23 pm
Location: Québec

Re: Computer languages engines implemented with

Post by mathmoi »

diep wrote:C++ practical is about 3 times slower than C for most programmers, except those who really know what they're doing AND regurarly print their C++ code to assembler, to see which mess the compiler created.
Wrong! If you understand how C++ objects work and how methods are called your C++ program should be as efficient as the same program written in C. Of course if you use polymorphism all over the place, even when it makes no sense of if you copy object where you would not copy them (the struct or the equivalents variables) in C you can write a pretty slow engine. This being said you can also write a slow engine in C.

C++ gives you some advantages over C. In general your code will get more reusable and possibly more readable. For example, in my engine I have a Move class that provides methods like .Piece() .PieceTaken() .Promotion() .From() .To(), etc. Some peoples will tell you such an object will slowdown your program since moves are used all over the place. In fact my Move class is just a wrapper around an unsigned int, so it's not slower than the traditional way to store moves. Where you have:

Code: Select all

from = move & FROM_MASK >> FROM_DELTA;
I have:

Code: Select all

from = move.From();
Thoses two pieces of codes compile to the same ASM instructions.

I also have an object for Piece, so I can do things like :

Code: Select all

color = move.Piece().Color();
Another place where C++ shine in my engine is in the move generations functions. Most engine have different function for normal moves and captures generation. Some engines also have two implementation of each of thoses, one for white and one for black. I have only one templated function that get compiled as four optimized function, something like this :

Code: Select all

template<bool genCaptures, EColor color>
Moves GenMovesRook&#40;Board b&#41;
&#123;
  fromBB = &#40;color == white ? b.white_rooks&#40;) &#58; b.black_rooks&#41;;
  target = &#40;genCaptures ? ... &#58; ...);
  ...
&#125;
When this code is compiled, four function are generated, one withe moves, one for black moves, one for white captures and one for black captures. The conditional code based on the templates parameter is optimized away by the compiler. I get the benefits of four function, but only have one function to maintain.

I use the same technique for my alpha-beta function :

Code: Select all

template<bool isRoot, bool isQuiescence>
unsigned int AlphaBeta&#40;...)
&#123;
  ...
&#125;
I maintain one function instead of two or three.