Computer languages engines implemented with
Moderators: hgm, Rebel, chrisw
Computer languages engines implemented with
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.
-
- Posts: 20943
- Joined: Mon Feb 27, 2006 7:30 pm
- Location: Birmingham, AL
Re: Computer languages engines implemented with
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 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.
Re: Computer languages engines implemented with
Thanks, Robert.bob wrote: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 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.
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?
-
- Posts: 1822
- Joined: Thu Mar 09, 2006 11:54 pm
- Location: The Netherlands
Re: Computer languages engines implemented with
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.kadr wrote:Thanks, Robert.bob wrote: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 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.
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 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
-
- Posts: 892
- Joined: Sun Nov 19, 2006 9:16 pm
- Location: Russia
Re: Computer languages engines implemented with
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...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.
-
- Posts: 20943
- Joined: Mon Feb 27, 2006 7:30 pm
- Location: Birmingham, AL
Re: Computer languages engines implemented with
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.kadr wrote:Thanks, Robert.bob wrote: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 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.
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?
-
- Posts: 778
- Joined: Sat Jul 01, 2006 7:11 am
Re: Computer languages engines implemented with
Another way of saying this is that it is easier to unknowingly write slow code in c++.
-
- Posts: 20943
- Joined: Mon Feb 27, 2006 7:30 pm
- Location: Birmingham, AL
Re: Computer languages engines implemented with
Yes, although it is also possible to unknowingly write slow code in C as well.jwes wrote:Another way of saying this is that it is easier to unknowingly write slow code in c++.
-
- Posts: 287
- Joined: Mon Mar 13, 2006 5:23 pm
- Location: Québec
Re: Computer languages engines implemented with
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.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.
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;
Code: Select all
from = move.From();
I also have an object for Piece, so I can do things like :
Code: Select all
color = move.Piece().Color();
Code: Select all
template<bool genCaptures, EColor color>
Moves GenMovesRook(Board b)
{
fromBB = (color == white ? b.white_rooks() : b.black_rooks);
target = (genCaptures ? ... : ...);
...
}
I use the same technique for my alpha-beta function :
Code: Select all
template<bool isRoot, bool isQuiescence>
unsigned int AlphaBeta(...)
{
...
}
Mathieu Pagé
mathieu@mathieupage.com
mathieu@mathieupage.com