[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Native PyGame method for automatically scaling inputs to a surface resolution?



On Fri, Sep 23, 2011 at 7:04 PM, Greg Ewing <greg.ewing@xxxxxxxxxxxxxxxx> wrote:
Christopher Night wrote:

I use wrappers. Let me point out there's a total of 9 functions in pygame.draw. You seem to be going to a lot of effort to avoid writing 9 one-line functions.

Don't forget that function calls are expensive in Python, as
is doing piecemeal arithmetic.

Most other graphics systems these days provide a facility for
applying a transformation to coordinates before drawing, and
I don't think it's unreasonable to suggest that PyGame should
do the same. Having it done for you in C would be more efficient
than doing it in Python.

While that is roughly true, it's a very, very general statement to the point where I would say that avoiding function calls on principle is premature optimization. Keep in mind that the operation you're wrapping - a draw call - is expensive in the first place. Anyway, a quick profile suggests that for a small circle you can potentially gain a 7% speedup by avoiding this function call, and a 14% speedup avoiding both the function and the arithmetic:

>>> import timeit
>>> setup = """import pygame
... pygame.init()
... s = pygame.display.set_mode((100, 100))
... def myCirc(surf, color, (x, y), r, width=0):
...     pygame.draw.circle(surf, color, (x/10, y/10), r/10, width/10)"""
>>> timeit.timeit("myCirc(s, (255,255,255), (500, 500), 400)", setup, number=100000)
4.9027900695800781
>>> pygame.quit()
>>> timeit.timeit("pygame.draw.circle(s, (255,255,255), (500/10, 500/10), 400/10)", setup, number=100000)
4.546515941619873
>>> pygame.quit()
>>> timeit.timeit("pygame.draw.circle(s, (255,255,255), (50, 50), 40)", setup, number=100000)
4.1960330009460449
>>> pygame.quit()

You can decide whether that's worth it for you to avoid this function call. For me, if my game is slow enough that I have to avoid function calls to get reasonable performance, it means I'm doing something else wrong. :) If performance is the goal here, I still think it's a large amount of effort for a relatively modest gain.

For what it's worth, I would also welcome native-pygame wrappers that apply a linear transformation. But whether pygame should have them wasn't the question, as I understood it. And I can scrape by without them.

-Christopher