stopwatch

This commit is contained in:
rektdie committed 2025-03-10 22:26:43 +01:00
1 parent 410d37be99
commit 325db852c4
4 files changed
+36 -33

No files matched your search

+3 -7
View File
@@ -1,7 +1,7 @@
#include "benchmark.h"
#include "search.h"
#include "stopwatch.h"
#include <array>
#include <chrono>
const std::array<std::string, 50> fenPositions = {
"rnbq1k1r/ppp1bppp/4pn2/8/2B5/2NP1N2/PPP2PPP/R1BQR1K1 b - - 2 8",
@@ -61,17 +61,13 @@ void RunBenchmark() {
int depth = 5;
benchStarted = true;
auto start = std::chrono::steady_clock::now();
Stopwatch sw;
for (int i = 0; i < fenPositions.size(); i++) {
board.SetByFen(fenPositions[i].c_str());
PVS(board, depth, -50000, 50000);
}
auto finish = std::chrono::steady_clock::now();
double elapsed_seconds = std::chrono::duration_cast<
std::chrono::duration<double>>(finish - start).count();
std::cout << nodes << " nodes " << int(nodes/elapsed_seconds) << " nps\n";
std::cout << nodes << " nodes " << int(nodes/sw.GetElapsedSec()) << " nps\n";
nodes = 0;
benchStarted = false;
}
+4 -8
View File
@@ -1,5 +1,5 @@
#include <chrono>
#include "perft.h"
#include "stopwatch.h"
U64 HelperPerft(Board board, int depth) {
GenerateMoves(board, board.sideToMove);
@@ -23,7 +23,7 @@ void Perft(Board &board, int depth) {
U64 totalNodes = 0;
auto start = std::chrono::steady_clock::now();
Stopwatch sw;
for (int i = 0; i < board.currentMoveIndex; i++) {
Board copy = board;
board.MakeMove(board.moveList[i]);
@@ -47,13 +47,9 @@ void Perft(Board &board, int depth) {
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 << "Total nodes: " << totalNodes << '\n';
std::cout << "Time took: " << elapsed_seconds << "s (";
std::cout << int(totalNodes / elapsed_seconds) << " nodes/sec)\n";
std::cout << "Time took: " << sw.GetElapsedSec() << "s (";
std::cout << int(totalNodes / sw.GetElapsedSec()) << " nodes/sec)\n";
}
+7 -18
View File
@@ -2,14 +2,13 @@
#include "movegen.h"
#include "evaluate.h"
#include <algorithm>
#include <chrono>
#include "tt.h"
#include "stopwatch.h"
U64 nodes = 0;
bool benchStarted = false;
static inline int ply = 0;
static inline auto timeStart = std::chrono::high_resolution_clock::now();
static inline int timeToSearch = 0;
static inline bool searchStopped = false;
static inline bool doingNullMove = false;
@@ -19,6 +18,8 @@ static inline int killerMoves[2][64];
inline PVLine pvLine;
Stopwatch sw;
static int ScoreMove(Board &board, Move &move) {
const int attackerType = board.GetPieceType(move.MoveFrom());
const int targetType = board.GetPieceType(move.MoveTo());
@@ -86,9 +87,7 @@ void ListScores(Board &board) {
static SearchResults Quiescence(Board board, int alpha, int beta) {
if (!benchStarted) {
auto currTime = std::chrono::high_resolution_clock::now();
int elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(currTime - timeStart).count();
if (elapsed >= timeToSearch) {
if (sw.GetElapsedMS() >= timeToSearch) {
StopSearch();
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) {
if (!benchStarted) {
auto currTime = std::chrono::high_resolution_clock::now();
int elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(currTime - timeStart).count();
if (elapsed >= timeToSearch) {
if (sw.GetElapsedMS() >= timeToSearch) {
StopSearch();
return 0;
}
@@ -255,7 +251,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta) {
// Iterative deepening
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 inc = board.sideToMove ? params.binc : params.winc;
@@ -297,14 +293,10 @@ static SearchResults ID(Board &board, SearchParams params) {
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 << "depth " << depth;
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 << " pv ";
pvLine.Print(0);
@@ -319,10 +311,7 @@ void SearchPosition(Board &board, SearchParams params) {
nodes = 0;
pvLine.Clear();
auto currTime = std::chrono::high_resolution_clock::now();
SearchResults results = ID(board, params);
U64 elapsed = std::chrono::duration_cast<
std::chrono::duration<U64>>(currTime - timeStart).count();
std::cout << "bestmove ";
results.bestMove.PrintMove();
+22
View File
@@ -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();
}
};