tstbenchmark.cc
Go to the documentation of this file.
1 // cwchessboard -- A C++ chessboard tool set for gtkmm
2 //
3 //! @file tstbenchmark.cc A program to measure the move generation speed.
4 //
5 // Copyright (C) 2008, by
6 //
7 // Carlo Wood, Run on IRC <carlo@alinoe.com>
8 // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt
9 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61
10 //
11 // This program is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 2 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 
24 #ifndef USE_PCH
25 #include "sys.h"
26 #include <iostream>
27 #include <cstdlib>
28 #include <ctime>
29 #include <iomanip>
30 #include <sstream>
31 #include <vector>
32 #include <sys/time.h>
33 #include "debug.h"
34 #endif
35 
36 #include "ChessPosition.h"
37 #include "PieceIterator.h"
38 #include "MoveIterator.h"
39 #include "ChessNotation.h"
40 
41 using namespace cwchess;
42 
43 void print_move(ChessPosition const& chess_position, Move const& move)
44 {
45  if (chess_position.to_move() == white)
46  std::cout << chess_position.full_move_number() << ". ";
47  std::ostringstream str;
48  str << ChessNotation(chess_position, move);
49  std::cout << std::setfill(' ') << std::setw(10) << std::left << str.str();
50  if (chess_position.to_move() == black)
51  std::cout << '\n';
52 }
53 
54 int main()
55 {
56  Debug(NAMESPACE_DEBUG::init());
57  Debug(libcw_do.off());
58 
59  time_t seed = 1220638382; // Use fixed seed for reproducibility. time(NULL);
60  //std::cout << "seed = " << seed << std::endl;
61  std::srand(seed);
62 
63  ChessPosition chess_position;
64  std::vector<Move> game;
65  Move moves[2048];
66  Move* move_ptr;
67  static int random_numbers[5000000];
68 
69  // Pre-calculate random numbers.
70  for (int i = 0; i < 5000000; ++i)
71  random_numbers[i] = std::rand();
72 
73  int games = 0;
74  int total_moves = 0;
75  int Move_count = 0;
76 
77  struct timeval before, after;
78  gettimeofday(&before, NULL);
79 
80  do
81  {
82  chess_position.initial_position();
83  //game.clear();
84  for(;;)
85  {
86  move_ptr = moves;
87  for (PieceIterator piece_iter(chess_position.piece_begin(chess_position.to_move())); // >
88  piece_iter != chess_position.piece_end(); // >- 20 ns
89  ++piece_iter) // >
90  {
91  MoveIterator const move_end;
92 #if 0
93  Move* move_save = move_ptr;
94  for (int i = 0; i < 100; ++i)
95  {
96  move_ptr = move_save;
97 #endif
98  for (MoveIterator move_iter(chess_position.move_begin(piece_iter.index())); // 216 ns (per executed move)
99  move_iter != move_end; // >- 161 ns (per executed move)
100  ++move_iter) // >
101  {
102  * move_ptr++ =* move_iter;
103  }
104 #if 0
105  }
106 #endif
107  }
108  int number_of_moves = move_ptr - moves;
109  Move_count += number_of_moves;
110  if (number_of_moves == 0)
111  break;
112  int mn = random_numbers[total_moves] % number_of_moves; // 12 ns.
113  ++total_moves;
114  //game.push_back(move);
115  if (chess_position.execute(moves[mn])) // 300 ns.
116  break;
117  }
118  ++games;
119  }
120  while (games < 10000);
121 
122  gettimeofday(&after, NULL);
123 
124 #if 0
125  chess_position.initial_position();
126  for (std::vector<Move>::iterator iter = game.begin(); iter != game.end(); ++iter)
127  {
128  //print_move(chess_position,* iter);
129  Move const& move(*iter);
130  print_move(chess_position, move);
131  chess_position.execute(move);
132  }
133  if (chess_position.to_move() == black)
134  std::cout << std::endl;
135 #endif
136 
137  std::cout << "Number of games played: " << games << "\nTotal number of moves played: " << total_moves << std::endl;
138 
139  timersub(&after,& before,& after);
140  uint64_t t = after.tv_sec;
141  t* = 1000000;
142  t += after.tv_usec;
143  std::cout << "Generated Move objects: " << (unsigned long)(Move_count / (t / 1000000.0) + 0.5) << " Moves/second." << std::endl;
144  std::cout << "Executed ply: " << (unsigned long)(total_moves / (t / 1000000.0) + 0.5) << " ply/second." << std::endl;
145  std::cout << "Computing time: " << ((double)t / total_moves) << " microseconds per executed move (ply)." << std::endl;
146 }
A namespace for all chess related objects that are not related to the GUI.
Definition: Array.h:39
bool execute(Move const& move)
Execute move move.
ColorData const white
A constant representing the color white.
Definition: Color.h:55
A chess move in a particular chess position.
Definition: Move.h:44
MoveIterator move_begin(Index const& index) const
Return an iterator to the first move of the piece at index index.
Non-mutable iterator over all moves of a given chess piece.
Definition: MoveIterator.h:58
A helper class to write other objects to an ostream.
Definition: ChessNotation.h:51
unsigned int full_move_number(void) const
Return the full move number.
PieceIterator piece_end(void) const
Return an iterator one beyond the last piece.
PieceIterator piece_begin(Color const& color) const
Return an iterator to the first piece of color color.
This file contains the definition of class PieceIterator.
A chess position.
Definition: ChessPosition.h:50
void to_move(Color const& color)
Explicitly set whose turn it is.
This file contains the declaration of class ChessPosition.
This file contains the declaration of class MoveIterator.
Non-mutable iterator over selective chess pieces in a chess position.
Definition: PieceIterator.h:42
void initial_position(void)
Set up the initial position.
This file contains the declaration of class ChessNotation.
ColorData const black
A constant representing the color black.
Definition: Color.h:53

Copyright © 2006 - 2010 Carlo Wood.  All rights reserved.