use numpy instead of bitmasks and get atleast 15x speedup, change default cols and rows to 128x96 for more space.

This commit is contained in:
csd4ni3l
2025-06-24 13:20:43 +02:00
parent c44dde3297
commit 88416c81e7
7 changed files with 119 additions and 85 deletions

View File

@@ -1,39 +1,27 @@
from utils.constants import ROWS, COLS, NEIGHBORS
TOTAL_CELLS = ROWS * COLS
from utils.constants import ROWS, COLS
import numpy as np
def create_numpy_grid():
return np.zeros((ROWS, COLS), dtype=np.uint8)
def get_index(row, col):
return row * COLS + col
def count_neighbors(grid):
padded = np.pad(grid, pad_width=1, mode='constant', constant_values=0)
neighbors = (
padded[0:-2, 0:-2] + # top-left
padded[0:-2, 1:-1] + # top
padded[0:-2, 2:] + # top-right
padded[1:-1, 0:-2] + # left
padded[1:-1, 2:] + # right
padded[2:, 0:-2] + # bottom-left
padded[2:, 1:-1] + # bottom
padded[2:, 2:] # bottom-right
)
return neighbors
def get_neighbors(cell_grid, neighbor_mask):
return (cell_grid & neighbor_mask).bit_count()
def unset_bit(number, i):
return number & ~(1 << i)
def set_bit(number, i):
return number | (1 << i)
def get_bit(number, i):
return (number >> i) & 1
def print_bits(n: int, width: int = 8):
print(f"{n:0{width}b}")
def create_zeroed_int(n):
zero_val = 0
bitmask = (1 << n) -1
return zero_val & bitmask
def precompute_neighbor_masks():
masks = [0] * TOTAL_CELLS
for row in range(ROWS):
for col in range(COLS):
index = get_index(row, col)
mask = 0
for dy, dx in NEIGHBORS:
ny, nx = row + dy, col + dx
if 0 <= ny < ROWS and 0 <= nx < COLS:
neighbor_index = get_index(ny, nx)
mask |= 1 << neighbor_index
masks[index] = mask
return masks
def update_generation(cell_grid: np.array):
neighbors = count_neighbors(cell_grid)
new_grid = ((cell_grid == 1) & ((neighbors == 2) | (neighbors == 3))) | \
((cell_grid == 0) & (neighbors == 3))
return new_grid.astype(np.uint8)