stopwatch
This commit is contained in:
1 parent
410d37be99
commit
325db852c4
4 files changed
+36
-33
No files matched your search
@@ -1,7 +1,7 @@
|
|||||||
#include "benchmark.h"
|
#include "benchmark.h"
|
||||||
#include "search.h"
|
#include "search.h"
|
||||||
|
#include "stopwatch.h"
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <chrono>
|
|
||||||
|
|
||||||
const std::array<std::string, 50> fenPositions = {
|
const std::array<std::string, 50> fenPositions = {
|
||||||
"rnbq1k1r/ppp1bppp/4pn2/8/2B5/2NP1N2/PPP2PPP/R1BQR1K1 b - - 2 8",
|
"rnbq1k1r/ppp1bppp/4pn2/8/2B5/2NP1N2/PPP2PPP/R1BQR1K1 b - - 2 8",
|
||||||
@@ -61,17 +61,13 @@ void RunBenchmark() {
|
|||||||
int depth = 5;
|
int depth = 5;
|
||||||
benchStarted = true;
|
benchStarted = true;
|
||||||
|
|
||||||
auto start = std::chrono::steady_clock::now();
|
Stopwatch sw;
|
||||||
for (int i = 0; i < fenPositions.size(); i++) {
|
for (int i = 0; i < fenPositions.size(); i++) {
|
||||||
board.SetByFen(fenPositions[i].c_str());
|
board.SetByFen(fenPositions[i].c_str());
|
||||||
PVS(board, depth, -50000, 50000);
|
PVS(board, depth, -50000, 50000);
|
||||||
}
|
}
|
||||||
auto finish = std::chrono::steady_clock::now();
|
|
||||||
|
|
||||||
double elapsed_seconds = std::chrono::duration_cast<
|
std::cout << nodes << " nodes " << int(nodes/sw.GetElapsedSec()) << " nps\n";
|
||||||
std::chrono::duration<double>>(finish - start).count();
|
|
||||||
|
|
||||||
std::cout << nodes << " nodes " << int(nodes/elapsed_seconds) << " nps\n";
|
|
||||||
nodes = 0;
|
nodes = 0;
|
||||||
benchStarted = false;
|
benchStarted = false;
|
||||||
}
|
}
|
||||||
+4
-8
@@ -1,5 +1,5 @@
|
|||||||
#include <chrono>
|
|
||||||
#include "perft.h"
|
#include "perft.h"
|
||||||
|
#include "stopwatch.h"
|
||||||
|
|
||||||
U64 HelperPerft(Board board, int depth) {
|
U64 HelperPerft(Board board, int depth) {
|
||||||
GenerateMoves(board, board.sideToMove);
|
GenerateMoves(board, board.sideToMove);
|
||||||
@@ -23,7 +23,7 @@ void Perft(Board &board, int depth) {
|
|||||||
|
|
||||||
U64 totalNodes = 0;
|
U64 totalNodes = 0;
|
||||||
|
|
||||||
auto start = std::chrono::steady_clock::now();
|
Stopwatch sw;
|
||||||
for (int i = 0; i < board.currentMoveIndex; i++) {
|
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
board.MakeMove(board.moveList[i]);
|
board.MakeMove(board.moveList[i]);
|
||||||
@@ -47,13 +47,9 @@ void Perft(Board &board, int depth) {
|
|||||||
|
|
||||||
std::cout << ": " << nodeCount << '\n';
|
std::cout << ": " << nodeCount << '\n';
|
||||||
}
|
}
|
||||||
auto finish = std::chrono::steady_clock::now();
|
|
||||||
|
|
||||||
double elapsed_seconds = std::chrono::duration_cast<
|
|
||||||
std::chrono::duration<double>>(finish - start).count();
|
|
||||||
|
|
||||||
std::cout << "\nDepth: " << depth << '\n';
|
std::cout << "\nDepth: " << depth << '\n';
|
||||||
std::cout << "Total nodes: " << totalNodes << '\n';
|
std::cout << "Total nodes: " << totalNodes << '\n';
|
||||||
std::cout << "Time took: " << elapsed_seconds << "s (";
|
std::cout << "Time took: " << sw.GetElapsedSec() << "s (";
|
||||||
std::cout << int(totalNodes / elapsed_seconds) << " nodes/sec)\n";
|
std::cout << int(totalNodes / sw.GetElapsedSec()) << " nodes/sec)\n";
|
||||||
}
|
}
|
||||||
+7
-18
@@ -2,14 +2,13 @@
|
|||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
#include "evaluate.h"
|
#include "evaluate.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <chrono>
|
|
||||||
#include "tt.h"
|
#include "tt.h"
|
||||||
|
#include "stopwatch.h"
|
||||||
|
|
||||||
U64 nodes = 0;
|
U64 nodes = 0;
|
||||||
bool benchStarted = false;
|
bool benchStarted = false;
|
||||||
|
|
||||||
static inline int ply = 0;
|
static inline int ply = 0;
|
||||||
static inline auto timeStart = std::chrono::high_resolution_clock::now();
|
|
||||||
static inline int timeToSearch = 0;
|
static inline int timeToSearch = 0;
|
||||||
static inline bool searchStopped = false;
|
static inline bool searchStopped = false;
|
||||||
static inline bool doingNullMove = false;
|
static inline bool doingNullMove = false;
|
||||||
@@ -19,6 +18,8 @@ static inline int killerMoves[2][64];
|
|||||||
|
|
||||||
inline PVLine pvLine;
|
inline PVLine pvLine;
|
||||||
|
|
||||||
|
Stopwatch sw;
|
||||||
|
|
||||||
static int ScoreMove(Board &board, Move &move) {
|
static int ScoreMove(Board &board, Move &move) {
|
||||||
const int attackerType = board.GetPieceType(move.MoveFrom());
|
const int attackerType = board.GetPieceType(move.MoveFrom());
|
||||||
const int targetType = board.GetPieceType(move.MoveTo());
|
const int targetType = board.GetPieceType(move.MoveTo());
|
||||||
@@ -86,9 +87,7 @@ void ListScores(Board &board) {
|
|||||||
|
|
||||||
static SearchResults Quiescence(Board board, int alpha, int beta) {
|
static SearchResults Quiescence(Board board, int alpha, int beta) {
|
||||||
if (!benchStarted) {
|
if (!benchStarted) {
|
||||||
auto currTime = std::chrono::high_resolution_clock::now();
|
if (sw.GetElapsedMS() >= timeToSearch) {
|
||||||
int elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(currTime - timeStart).count();
|
|
||||||
if (elapsed >= timeToSearch) {
|
|
||||||
StopSearch();
|
StopSearch();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -136,10 +135,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta) {
|
|||||||
|
|
||||||
SearchResults PVS(Board board, int depth, int alpha, int beta) {
|
SearchResults PVS(Board board, int depth, int alpha, int beta) {
|
||||||
if (!benchStarted) {
|
if (!benchStarted) {
|
||||||
auto currTime = std::chrono::high_resolution_clock::now();
|
if (sw.GetElapsedMS() >= timeToSearch) {
|
||||||
int elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(currTime - timeStart).count();
|
|
||||||
|
|
||||||
if (elapsed >= timeToSearch) {
|
|
||||||
StopSearch();
|
StopSearch();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -255,7 +251,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta) {
|
|||||||
|
|
||||||
// Iterative deepening
|
// Iterative deepening
|
||||||
static SearchResults ID(Board &board, SearchParams params) {
|
static SearchResults ID(Board &board, SearchParams params) {
|
||||||
timeStart = std::chrono::high_resolution_clock::now();
|
sw.Restart();
|
||||||
|
|
||||||
int fullTime = board.sideToMove ? params.btime : params.wtime;
|
int fullTime = board.sideToMove ? params.btime : params.wtime;
|
||||||
int inc = board.sideToMove ? params.binc : params.winc;
|
int inc = board.sideToMove ? params.binc : params.winc;
|
||||||
@@ -297,14 +293,10 @@ static SearchResults ID(Board &board, SearchParams params) {
|
|||||||
|
|
||||||
if (searchStopped) break;
|
if (searchStopped) break;
|
||||||
|
|
||||||
auto currTime = std::chrono::high_resolution_clock::now();
|
|
||||||
double elapsed = std::chrono::duration_cast<
|
|
||||||
std::chrono::duration<double>>(currTime - timeStart).count();
|
|
||||||
|
|
||||||
std::cout << "info ";
|
std::cout << "info ";
|
||||||
std::cout << "depth " << depth;
|
std::cout << "depth " << depth;
|
||||||
std::cout << " score cp " << safeResults.score;
|
std::cout << " score cp " << safeResults.score;
|
||||||
std::cout << " nodes " << nodes << " nps " << int(nodes/elapsed);
|
std::cout << " nodes " << nodes << " nps " << int(nodes/sw.GetElapsedSec());
|
||||||
std::cout << " hashfull " << TT.GetUsedPercentage();
|
std::cout << " hashfull " << TT.GetUsedPercentage();
|
||||||
std::cout << " pv ";
|
std::cout << " pv ";
|
||||||
pvLine.Print(0);
|
pvLine.Print(0);
|
||||||
@@ -319,10 +311,7 @@ void SearchPosition(Board &board, SearchParams params) {
|
|||||||
nodes = 0;
|
nodes = 0;
|
||||||
pvLine.Clear();
|
pvLine.Clear();
|
||||||
|
|
||||||
auto currTime = std::chrono::high_resolution_clock::now();
|
|
||||||
SearchResults results = ID(board, params);
|
SearchResults results = ID(board, params);
|
||||||
U64 elapsed = std::chrono::duration_cast<
|
|
||||||
std::chrono::duration<U64>>(currTime - timeStart).count();
|
|
||||||
|
|
||||||
std::cout << "bestmove ";
|
std::cout << "bestmove ";
|
||||||
results.bestMove.PrintMove();
|
results.bestMove.PrintMove();
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
class Stopwatch {
|
||||||
|
private:
|
||||||
|
std::chrono::high_resolution_clock::time_point start =
|
||||||
|
std::chrono::high_resolution_clock::now();
|
||||||
|
public:
|
||||||
|
void Restart() {
|
||||||
|
start = std::chrono::high_resolution_clock::now();
|
||||||
|
}
|
||||||
|
|
||||||
|
int GetElapsedMS() {
|
||||||
|
auto current = std::chrono::high_resolution_clock::now();
|
||||||
|
return std::chrono::duration_cast<std::chrono::milliseconds>(current - start).count();
|
||||||
|
}
|
||||||
|
|
||||||
|
double GetElapsedSec() {
|
||||||
|
auto current = std::chrono::high_resolution_clock::now();
|
||||||
|
return std::chrono::duration_cast<
|
||||||
|
std::chrono::duration<double>>(current - start).count();
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in new issue
Block a user