Skip to content

Commit

Permalink
6.14ms -> 5.38 ms. Prefer logic benchmark speed over app speed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jumbub committed Oct 6, 2021
1 parent 80e8660 commit b075a2f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
4 changes: 2 additions & 2 deletions results/benchmark.txt
Original file line number Diff line number Diff line change
@@ -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
5 changes: 2 additions & 3 deletions src/board/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
#include <memory>
#include <tuple>

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<Cell*, unsigned int, unsigned int>;
19 changes: 6 additions & 13 deletions src/board/next.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand All @@ -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];
Expand Down
16 changes: 15 additions & 1 deletion src/board/sdl.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "sdl.h"
#include <vector>
#include <cstring>
#include "../util/profile.h"
#include "generate.h"

Expand All @@ -20,12 +21,25 @@ 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(
texture, NULL, &input[0], (long unsigned int)width * sizeof(Uint32));

SDL_RenderCopy(renderer, texture, nullptr, nullptr);
SDL_RenderPresent(renderer);

delete[] input;
}

0 comments on commit b075a2f

Please sign in to comment.