From b075a2f68ded65e3828d18e972e9a0fd0a24d596 Mon Sep 17 00:00:00 2001 From: Jamie Bray Date: Thu, 7 Oct 2021 00:05:01 +0800 Subject: [PATCH] 6.14ms -> 5.38 ms. Prefer logic benchmark speed over app speed. --- results/benchmark.txt | 4 ++-- src/board/board.h | 5 ++--- src/board/next.cpp | 19 ++++++------------- src/board/sdl.cpp | 16 +++++++++++++++- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/results/benchmark.txt b/results/benchmark.txt index 520c08b..0cc2493 100644 --- a/results/benchmark.txt +++ b/results/benchmark.txt @@ -1,5 +1,5 @@ --------------------------------------------------------------------------------------------- Benchmark Time CPU Iterations --------------------------------------------------------------------------------------------- -BM_NextBoard/min_time:2.000/process_time/real_time 6.14 ms 24.0 ms 451 -BM_RenderBoard/process_time/real_time 7.12 ms 6.97 ms 91 +BM_NextBoard/min_time:2.000/process_time/real_time 5.38 ms 20.6 ms 516 +BM_RenderBoard/process_time/real_time 11.9 ms 11.9 ms 53 diff --git a/src/board/board.h b/src/board/board.h index 7c61be0..b8f89ad 100644 --- a/src/board/board.h +++ b/src/board/board.h @@ -4,9 +4,8 @@ #include #include -using Cell = unsigned int; // Should match SDL::Uint32, otherwise the rendering - // will not work as intended -const Cell ALIVE = UINT32_MAX; // Should match SDL::SDL_UINT32_MAX, " +using Cell = unsigned int; +const Cell ALIVE = 1; const Cell DEAD = 0; using Board = std::tuple; diff --git a/src/board/next.cpp b/src/board/next.cpp index 53876d6..c835272 100644 --- a/src/board/next.cpp +++ b/src/board/next.cpp @@ -19,10 +19,6 @@ void setThreads(unsigned int n) { THREAD_COUNT = std::max(n, (unsigned int)1); } -inline const auto maxToOne(Cell n) { - return (1 - (n + 1)); -} - void nextBoardSection( const unsigned int startY, const unsigned int endY, @@ -38,7 +34,7 @@ void nextBoardSection( const auto endI = endY * width; for (unsigned int i = startY * width; i < endI; i++) { const unsigned int x = i % width; - auto currentStateBool = maxToOne(input[i]); + auto currentStateBool = input[i]; // Slide neighbours if (x == 0) { @@ -61,18 +57,15 @@ void nextBoardSection( if (neighbours[0] == UINT32_MAX) { const auto previousX = (x - 1 + width) % width; - neighbours[0] = maxToOne(input[yBelowBase + previousX]) + - maxToOne(input[yBase + previousX]) + - maxToOne(input[yAboveBase + previousX]); + neighbours[0] = input[yBelowBase + previousX] + input[yBase + previousX] + + input[yAboveBase + previousX]; } if (neighbours[1] == UINT32_MAX) { - neighbours[1] = - maxToOne(input[yBelowBase + x]) + maxToOne(input[yAboveBase + x]); + neighbours[1] = input[yBelowBase + x] + input[yAboveBase + x]; } const auto nextX = (x + 1) % width; - neighbours[2] = maxToOne(input[yBelowBase + nextX]) + - maxToOne(input[yBase + nextX]) + - maxToOne(input[yAboveBase + nextX]); + neighbours[2] = input[yBelowBase + nextX] + input[yBase + nextX] + + input[yAboveBase + nextX]; // Compute new cell state const auto totalNeighbours = neighbours[0] + neighbours[1] + neighbours[2]; diff --git a/src/board/sdl.cpp b/src/board/sdl.cpp index 318d57a..ffa2e19 100644 --- a/src/board/sdl.cpp +++ b/src/board/sdl.cpp @@ -1,5 +1,6 @@ #include "sdl.h" #include +#include #include "../util/profile.h" #include "generate.h" @@ -20,7 +21,18 @@ void renderBoardSdl( const Board board, SDL_Renderer* renderer, SDL_Texture* texture) { - const auto& [input, width, height] = board; + const auto& [rawInput, width, height] = board; + Cell* input = new Cell[width*height]; + + // I would LOVE it if someone could figure out how to create a "bool" pixel + // format to use with SDL. Then I wouldn't need to do this memcpy trash. + std::memcpy(input, rawInput, width*height*sizeof(Cell)); + + for (unsigned int i = 0; i < width * height; i ++) { + if (input[i] == ALIVE) { + input[i] = UINT32_MAX; + } + } static_assert(sizeof(input[0]) == sizeof(Uint32)); SDL_UpdateTexture( @@ -28,4 +40,6 @@ void renderBoardSdl( SDL_RenderCopy(renderer, texture, nullptr, nullptr); SDL_RenderPresent(renderer); + + delete[] input; }