Skip to content

Commit

Permalink
15ms -> 12ms. Replaced ternaries with math expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jumbub committed Oct 3, 2021
1 parent cf3c9b9 commit 21bd487
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 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/process_time/real_time 15.2 ms 51.0 ms 41
BM_RenderBoard/process_time/real_time 7.04 ms 7.03 ms 90
BM_NextBoard/process_time/real_time 12.2 ms 45.2 ms 54
BM_RenderBoard/process_time/real_time 7.13 ms 7.04 ms 90
14 changes: 7 additions & 7 deletions src/board/next.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@ void nextBoardSection(const int startY, const int endY, const Board &board,
if (neighbours[0] == -1) {
const auto previousX = (x - 1 + width) % width;

neighbours[0] = (input[yBelowBase + previousX] ? 1 : 0) +
(input[yBase + previousX] ? 1 : 0) +
(input[yAboveBase + previousX] ? 1 : 0);
neighbours[0] = (1 - (input[yBelowBase + previousX] + 1)) +
(1 - (input[yBase + previousX] + 1)) +
(1 - (input[yAboveBase + previousX] + 1));
}
if (neighbours[1] == -1) {
neighbours[1] =
(input[yBelowBase + x] ? 1 : 0) + (input[yAboveBase + x] ? 1 : 0);
(1 - (input[yBelowBase + x] + 1)) + (1 - (input[yAboveBase + x] + 1));
}
const auto nextX = (x + 1) % width;
neighbours[2] = (input[yBelowBase + nextX] ? 1 : 0) +
(input[yBase + nextX] ? 1 : 0) +
(input[yAboveBase + nextX] ? 1 : 0);
neighbours[2] = (1 - (input[yBelowBase + nextX] + 1)) +
(1 - (input[yBase + nextX] + 1)) +
(1 - (input[yAboveBase + nextX] + 1));

// Compute new cell state
const int totalNeighbours = neighbours[0] + neighbours[1] + neighbours[2];
Expand Down

0 comments on commit 21bd487

Please sign in to comment.