Skip to content

Commit

Permalink
52ms -> 46ms. Render the board in parallel to generating the next one.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jumbub committed Oct 3, 2021
1 parent 289b948 commit 2619b41
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 26 deletions.
4 changes: 2 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CC = g++

COMPILER_FLAGS = -Wall -std=c++2a
COMPILER_FLAGS_BENCHMARK = $(COMPILER_FLAGS) -isystem benchmark/include -Lbenchmark/build/src -lbenchmark -lpthread
COMPILER_FLAGS = -Wall -std=c++2a -lpthread
COMPILER_FLAGS_BENCHMARK = $(COMPILER_FLAGS) -isystem benchmark/include -Lbenchmark/build/src -lbenchmark
COMPILER_FLAGS_PROFILE = $(COMPILER_FLAGS) -pg
COMPILER_FLAGS_DEBUG = $(COMPILER_FLAGS) -ggdb

Expand Down
10 changes: 5 additions & 5 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:5.000 50.4 ms 50.4 ms 125
BM_RenderNextBoard/min_time:5.000 55.8 ms 55.8 ms 111
------------------------------------------------------------------------
Benchmark Time CPU Iterations
------------------------------------------------------------------------
BM_NextBoard/min_time:5.000 50.0 ms 49.9 ms 126
BM_RenderBoard/min_time:5.000 6.96 ms 6.95 ms 1006
5 changes: 2 additions & 3 deletions src/entrypoints/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static void BM_NextBoard(benchmark::State &state) {
free(get<0>(board));
}

static void BM_RenderNextBoard(benchmark::State &state) {
static void BM_RenderBoard(benchmark::State &state) {
auto board = benchmarkBoard(TEST_WIDTH, TEST_HEIGHT);

// Initialize graphics
Expand All @@ -29,7 +29,6 @@ static void BM_RenderNextBoard(benchmark::State &state) {
SDL_TEXTUREACCESS_STATIC, TEST_WIDTH, TEST_HEIGHT);

for (auto _ : state) {
board = nextBoard(board);
renderBoardSdl(board, renderer, texture);
}

Expand All @@ -43,6 +42,6 @@ static void BM_RenderNextBoard(benchmark::State &state) {
}

BENCHMARK(BM_NextBoard)->Unit(benchmark::kMillisecond)->MinTime(5);
BENCHMARK(BM_RenderNextBoard)->Unit(benchmark::kMillisecond)->MinTime(5);
BENCHMARK(BM_RenderBoard)->Unit(benchmark::kMillisecond)->MinTime(5);

BENCHMARK_MAIN();
48 changes: 32 additions & 16 deletions src/entrypoints/interactive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_timer.h>
#include <iostream>
#include <thread>
#include <thread>
#include <future>

void nextBoardThreaded(Board board, std::promise<Board> promise) {
promise.set_value(nextBoard(board));
}

int main() {
// Initialize SDL
Expand All @@ -24,12 +31,17 @@ int main() {
SDL_TEXTUREACCESS_STATIC, width, height);

// Generate initial board
auto p1 = startProfiling();
auto board = boardForSdlWindow(window);
stopProfiling(p1, "Generated first board");

bool running = true;
bool recreateBoard = false;
while (running) {
auto loopTimer = startProfiling();

std::promise<Board> nextBoardPromise;
auto nextBoardFuture = nextBoardPromise.get_future();
std::thread nextBoardThread(nextBoardThreaded, board, std::move(nextBoardPromise));

while (SDL_PollEvent(&event)) {
// Exit when told, or Escape is pressed
if ((event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) ||
Expand All @@ -40,24 +52,28 @@ int main() {
// Re-create board when Enter is pressed
else if (event.type == SDL_KEYDOWN &&
event.key.keysym.scancode == SDL_SCANCODE_RETURN) {

free(get<0>(board));
board = boardForSdlWindow(window);
int width, height;
SDL_GetWindowSize(window, &width, &height);
SDL_DestroyTexture(texture);
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STATIC, width, height);
recreateBoard = true;
}
}

auto p2 = startProfiling();
board = nextBoard(board);
stopProfiling(p2, "Calculated next board");

auto p3 = startProfiling();
renderBoardSdl(board, renderer, texture);
stopProfiling(p3, "Rendered next board");

nextBoardThread.join();
board = nextBoardFuture.get();

// Re-create board when computation is complete
if (recreateBoard) {
free(get<0>(board));
board = boardForSdlWindow(window);
int width, height;
SDL_GetWindowSize(window, &width, &height);
SDL_DestroyTexture(texture);
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STATIC, width, height);
recreateBoard = false;
}

stopProfiling(loopTimer, "Done loop");
}

free(get<0>(board));
Expand Down

0 comments on commit 2619b41

Please sign in to comment.