Author: Russell Reagan
Date: 12:13:09 06/08/02
Go up one level in this thread
I learned what was called "C++" in high school. In actuallity it was C, but we used cout instead of printf, so technically we were using C++ I suppose. I couldn't have told you what the difference in the languages was though. In college I finally learned all of the distinct features of C++. When deciding on which language to write my chess program in, I have gone back and forth between C and C++. At first I chose C because I had these great plans that I was going to analyze the assembly code that was generated and work on optimizing, and I had noticed that the the ASM code that the compiler generated for a C program was highly more readable than the code generated for a C++ program, even for the exact same program. Ultimately I decided on C++ because I valued the ease of development and the ability to remain flexible as I change my mind about which board representation I want to use and other decisions which would cause significant changes to be made in a C program. Basically I valued flexibility over absolute speed, and I really doubt that if I would have chosen C that my program would have been any faster. One thing I definitely did not like about C was that I would take my data structures, and when attempting to read in a FEN string or create a PGN from my games, it seemed like I was just hacking those features into my program, rather than having planned for them and allowed for an easy addition without it being messy. My solution was to use classes for almost everything. Position, move, square, piece, castling rights, whatever. The result is that each of those objects knows how to handle it's duties and it doesn't have to worry about other objects. Each of my objects has a nice toString() method that I really enjoy. As far as speed is concerned, most of the methods are inlined anyway, so it will act just like a C struct in terms of efficiency. I see no reason for there to be any major speed difference. One possible area is that I chose to use the STL vector over an array for robustness reasons. That might cause a little speed hit (but maybe not), and if it does, that's fine with me because I don't have to worry about the occasional game that goes into the hundreds of moves, or the occasional position that has hundreds of legal moves, and so on. The final thing that I really enjoy about using classes instead of structs in C is that I can verify that the data isn't going to cause bad things to happen. I added assert()'s to each class to verify that the class's data is valid. It doesn't cause any speed hit because it only verifies the data in debug mode. In release mode the assert()'s aren't present, and the accessor functions are inlined, so I see no area where there will be a major speed hit. The result of all of this is that my program will run just as fast as it would have in C (okay, maybe 1% slower, which I could care less about), and I now have a method to verify data during runtime, I can easily add new things such as PGN support without having to "hack" anything into my program, and I can change how a data structure works internally without it affecting the rest of the program. So if I chose to go from 0x88 to bitboards, I could do so without having to rewrite half my program. I'm very happy with my choice. Russell
This page took 0.01 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.