1

Hardware: iMac with a 4.2GHz i7, 40 GB of DDR4 RAM, and a Radeon Pro 580 8192 MB

-> Curiously, my updated code posted below ran at only 1.6 FPS on my iMac

I started making a game in pygame that became unusably slow. I discovered that having a larger window decreased the fps significantly, even when nothing was being displayed. To demonstrate this, I created the simple program below. Every iteration, it increases the window size slightly.

At first, the game window (with nothing displayed on it) runs at about 150 fps at 100x100 pixels. By the time it increases to 1000x1000 pixels it's running at just 2 fps.

What am I doing wrong? I will have to give up on making my game if the fps is this low. Thanks for the help.

import pygame, time
pygame.init()

display_width = 100
display_height = 100
win = pygame.display.set_mode((display_width, display_height))
clock = pygame.time.Clock()

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    win.fill((0, 0, 0)) #fills over pre-existing elements

    pygame.display.update()
    clock.tick()
    print(clock.get_fps())
    display_width += 1
    display_height += 1
    win = pygame.display.set_mode((display_width, display_height))

print('QUIT')
pygame.quit()

UPDATE: I have been notified that updating the display dimensions every iteration makes this an unfair test for performance. This is why I have updated the code below. This code initializes the window size at 2000x2000 and constantly updates it as a blank screen. However, this still only achieves 6 FPS with nothing even on it. Why is this?

import pygame, time
pygame.init()

display_width = 2000
display_height = 2000
win = pygame.display.set_mode((display_width, display_height))
clock = pygame.time.Clock()

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    win.fill((0, 0, 0)) #fills over pre-existing elements

    pygame.display.update()
    clock.tick()
    print(clock.get_fps())


print('QUIT')
pygame.quit()
Davis Herring
  • 36,443
  • 4
  • 48
  • 76
  • 1
    I profiled this code and 65% of the time is in `pygame.display.set_mode((display_width, display_height))` so this is really not a fair benchmark. Are you reseting the mode to the same value on every frame in your real code? – Keatinge May 21 '19 at 03:26
  • What does your actual game code look like? This code snipet doesn't provide enough context about where your problem could be. This code is just an infinite loop of increasing the display size and re-initiating it. Is this the code that is causing the problem. If so, what is your intent behind this code - it isn't a design pattern you would ever want in practice. – RD3 May 21 '19 at 03:33
  • Thanks for pointing out that resizing the window every time is inefficient for testing. I have updated the code above to address this. However, the performance is still slow with a fixed window size. – Louie Kotler May 21 '19 at 13:16
  • What type of system is this running on? Specifically the CPU, RAM, GPU and VRAM. – Matt May 21 '19 at 14:57
  • I have updated my question with the hardware listed at the top. – Louie Kotler May 23 '19 at 03:21

1 Answers1

0

pygame.display.set_mode is a relatively expensive operation, and you seem to be doing that on every single iteration of the loop.

You can fix it by first making a function with the screen modifying code, so...

def increase_screen():
    display_width += 1
    display_height += 1
    win = pygame.display.set_mode((display_width, display_height))

and then, you can bind it to a timer to only execute every second or so:

# Somewhere earlier in the code
ticks = 0

# Somewhere later in the update part of the loop
ticks += 1

if ticks == 1000:
    increase_screen()
    ticks = 0

That should decrease the rate its running, and therefore fix the lag problem.

Mercury Platinum
  • 1,549
  • 1
  • 15
  • 28
  • I should have been more clear. The goal is not to continually increase the screen size. I was just using this as a benchmark to test the FPS at each window dimension. However, when I set the window size to a fixed 2000x2000, it only runs at 6 FPS. I have updated the code above that has this problem. – Louie Kotler May 21 '19 at 13:17
  • Your new code seems to run at 37 FPS on my laptop. Are you sure that it is not a problem with your computers performance? – Mercury Platinum May 21 '19 at 13:34
  • I ran it on my iMac with a 4.2GHz i7, 40 GB of DDR4 RAM, Radeon Pro 580 8192 MB. Not a slow computer but it ran at 1.6 FPS which is even slower that my MacBook Pro at about 6 FPS. – Louie Kotler May 21 '19 at 19:52