pythonpygamepysdl2

My quick test indicates pygame is 100 times faster than pysdl2, whats going on?


So I have been making a game in pygame, but performance is horrible. After a lot of searching I discovered that pygame is built around SDL and that SDL2 has GPU support, so I wanted to see if using something SDL2 based would improve performance. In comparing pygame with the pySDL2 library that pygame is about 100 times faster... Surely I am overlooking something?

test_SDL2.py:

import os
os.environ["PYSDL2_DLL_PATH"] = os.path.dirname(os.path.abspath(__file__))

import sys, sdl2, sdl2.ext, time

sdl2.ext.init()
window = sdl2.ext.Window("test", size=(800, 600))
window.show()
factory = sdl2.ext.SpriteFactory(sdl2.ext.SOFTWARE)
sprite = factory.from_image("c:\\game\\gfx\\plus.png")

spriterenderer = factory.create_sprite_render_system(window)
while True:
    begin = time.time()
    for i in xrange(100):
        spriterenderer.render(sprite)
    print time.time() - begin

window.refresh()

test_pygame.py:

import pygame, time

img = pygame.image.load('gfx/plus.png')

screen = pygame.display.set_mode((800, 600))
while True:
    begin = time.time()
    for i in xrange(100):
        screen.blit(img, (i,i))
    print time.time() - begin
    pygame.display.flip()

Solution

  • Caveat: I just started with SDL2 a few days ago [in C].

    In pygame, you're doing 100 blits but only one screen update with pygame.display.flip()

    In the SDL2 version, your doing 100 render operations, which I think do a blit and a screen update. There is no separate call to update the screen as there is with pygame, so if you're getting any output, this means that the render is a combined operation.

    To check, remove the flip() from pygame and I bet you get no output.

    Then, move the flip() inside the loop and I bet the performance will be similar.