diff --git a/source/benchmark.cpp b/source/benchmark.cpp index 66ab7ae..66753dd 100644 --- a/source/benchmark.cpp +++ b/source/benchmark.cpp @@ -1,7 +1,7 @@ #include "benchmark.h" #include "search.h" +#include "stopwatch.h" #include -#include const std::array 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>(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; } diff --git a/source/perft.cpp b/source/perft.cpp index bf0f3e1..fab6939 100644 --- a/source/perft.cpp +++ b/source/perft.cpp @@ -1,5 +1,5 @@ -#include #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>(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"; } diff --git a/source/search.cpp b/source/search.cpp index 6e3c9ec..5e92f63 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -2,14 +2,13 @@ #include "movegen.h" #include "evaluate.h" #include -#include #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(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(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>(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>(currTime - timeStart).count(); std::cout << "bestmove "; results.bestMove.PrintMove(); diff --git a/source/stopwatch.h b/source/stopwatch.h new file mode 100644 index 0000000..211f851 --- /dev/null +++ b/source/stopwatch.h @@ -0,0 +1,22 @@ +#include + +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(current - start).count(); + } + + double GetElapsedSec() { + auto current = std::chrono::high_resolution_clock::now(); + return std::chrono::duration_cast< + std::chrono::duration>(current - start).count(); + } +}; \ No newline at end of file