I was wondering what I could do to increase the overall speed of changing individual values in a NumPy array?
class Draw:
def __init__(self, game, pygame):
self.pygame = pygame
self.game = game
self.display = np.array([[random_color() for x in range(self.game.settings.PIX_ARRAY_HEIGHT)] for y in range(self.game.settings.PIX_ARRAY_WIDTH)])
def draw_screen(self):
self.pygame.surfarray.make_surface(self.display)
surface = self.pygame.surfarray.make_surface(self.display)
surface = self.pygame.transform.scale(surface, (self.game.settings.SCREEN_WIDTH, self.game.settings.SCREEN_HEIGHT))
self.game.screen.blit(surface, (0, 0))
self.pygame.display.update()
def pixel(self, x, y, color, alpha=1):
if x >= 0 and x < self.game.settings.PIX_ARRAY_WIDTH and y >= 0 and y < self.game.settings.PIX_ARRAY_HEIGHT:
self.display[int(x), int(y)] = color if alpha == 1 else self.mix_pixels(color, self.display[int(x), int(y)], alpha)
def rect(self, x, y, w, h, color):
threads = []
for i in range(x, x + w):
for j in range(y, y + h):
x = threading.Thread(target=self.pixel, args=(i,j,color,))
threads.append(x)
x.start()
I tried threading to increase the speed of overwriting pixels in the NumPy array but that seemed to decrease the overall speed.
I have tried to use numbas's njit and jit tool to pre-compile the code, but it refused to do so.
when a 100 x 100 rect is drawn the fps drops from 160+ to just over 3 without any threading with threading it goes to about 0.13 fps.