Skip to content

Commit 2619b41

Browse files
committed
52ms -> 46ms. Render the board in parallel to generating the next one.
1 parent 289b948 commit 2619b41

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
lines changed

makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CC = g++
22

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

results/benchmark.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
----------------------------------------------------------------------------
2-
Benchmark Time CPU Iterations
3-
----------------------------------------------------------------------------
4-
BM_NextBoard/min_time:5.000 50.4 ms 50.4 ms 125
5-
BM_RenderNextBoard/min_time:5.000 55.8 ms 55.8 ms 111
1+
------------------------------------------------------------------------
2+
Benchmark Time CPU Iterations
3+
------------------------------------------------------------------------
4+
BM_NextBoard/min_time:5.000 50.0 ms 49.9 ms 126
5+
BM_RenderBoard/min_time:5.000 6.96 ms 6.95 ms 1006

src/entrypoints/benchmark.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static void BM_NextBoard(benchmark::State &state) {
1717
free(get<0>(board));
1818
}
1919

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

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

3131
for (auto _ : state) {
32-
board = nextBoard(board);
3332
renderBoardSdl(board, renderer, texture);
3433
}
3534

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

4544
BENCHMARK(BM_NextBoard)->Unit(benchmark::kMillisecond)->MinTime(5);
46-
BENCHMARK(BM_RenderNextBoard)->Unit(benchmark::kMillisecond)->MinTime(5);
45+
BENCHMARK(BM_RenderBoard)->Unit(benchmark::kMillisecond)->MinTime(5);
4746

4847
BENCHMARK_MAIN();

src/entrypoints/interactive.cpp

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
#include <SDL2/SDL.h>
66
#include <SDL2/SDL_timer.h>
77
#include <iostream>
8+
#include <thread>
9+
#include <thread>
10+
#include <future>
11+
12+
void nextBoardThreaded(Board board, std::promise<Board> promise) {
13+
promise.set_value(nextBoard(board));
14+
}
815

916
int main() {
1017
// Initialize SDL
@@ -24,12 +31,17 @@ int main() {
2431
SDL_TEXTUREACCESS_STATIC, width, height);
2532

2633
// Generate initial board
27-
auto p1 = startProfiling();
2834
auto board = boardForSdlWindow(window);
29-
stopProfiling(p1, "Generated first board");
3035

3136
bool running = true;
37+
bool recreateBoard = false;
3238
while (running) {
39+
auto loopTimer = startProfiling();
40+
41+
std::promise<Board> nextBoardPromise;
42+
auto nextBoardFuture = nextBoardPromise.get_future();
43+
std::thread nextBoardThread(nextBoardThreaded, board, std::move(nextBoardPromise));
44+
3345
while (SDL_PollEvent(&event)) {
3446
// Exit when told, or Escape is pressed
3547
if ((event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) ||
@@ -40,24 +52,28 @@ int main() {
4052
// Re-create board when Enter is pressed
4153
else if (event.type == SDL_KEYDOWN &&
4254
event.key.keysym.scancode == SDL_SCANCODE_RETURN) {
43-
44-
free(get<0>(board));
45-
board = boardForSdlWindow(window);
46-
int width, height;
47-
SDL_GetWindowSize(window, &width, &height);
48-
SDL_DestroyTexture(texture);
49-
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
50-
SDL_TEXTUREACCESS_STATIC, width, height);
55+
recreateBoard = true;
5156
}
5257
}
5358

54-
auto p2 = startProfiling();
55-
board = nextBoard(board);
56-
stopProfiling(p2, "Calculated next board");
57-
58-
auto p3 = startProfiling();
5959
renderBoardSdl(board, renderer, texture);
60-
stopProfiling(p3, "Rendered next board");
60+
61+
nextBoardThread.join();
62+
board = nextBoardFuture.get();
63+
64+
// Re-create board when computation is complete
65+
if (recreateBoard) {
66+
free(get<0>(board));
67+
board = boardForSdlWindow(window);
68+
int width, height;
69+
SDL_GetWindowSize(window, &width, &height);
70+
SDL_DestroyTexture(texture);
71+
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
72+
SDL_TEXTUREACCESS_STATIC, width, height);
73+
recreateBoard = false;
74+
}
75+
76+
stopProfiling(loopTimer, "Done loop");
6177
}
6278

6379
free(get<0>(board));

0 commit comments

Comments
 (0)