[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Benchmarking alpha blits (was Re: How do I make a image transparent?)
On Sep 15, 2007, at 12:14 AM, Ethan Glasser-Camp wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Casey Duncan wrote:
So convert_alpha() is a huge win, and RLEACCEL is some icing on the
cake. Granted this is just one example image, but I thought it was
worth
sharing.
Great work, thanks!
1) Can I see the code?
I worked up a better version (attached). It takes two arguments, a
path to the image to load for the blit test and the number blits used
per test (defaults to 10k)
import sys
import pygame
from timeit import Timer
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((800, 600), FULLSCREEN | HWSURFACE | DOUBLEBUF, 32)
image = pygame.image.load(sys.argv[1])
if len(sys.argv) > 2:
blits = int(sys.argv[2])
else:
blits = 10000
print image, blits, 'blits'
t = Timer("screen.blit(image, (0, 0))", "from __main__ import screen, image")
print 'no optimization', t.timeit(blits)
pygame.display.flip()
converted = image.convert()
t = Timer("screen.blit(converted, (100, 0))", "from __main__ import screen, converted")
print 'convert only', t.timeit(blits)
pygame.display.flip()
converted.set_colorkey(converted.get_at((0,0)))
t = Timer("screen.blit(converted, (200, 0))", "from __main__ import screen, converted")
print 'colorkey no RLEACCEL', t.timeit(blits)
pygame.display.flip()
converted.set_colorkey(converted.get_at((0,0)), pygame.RLEACCEL)
t = Timer("screen.blit(converted, (300, 0))", "from __main__ import screen, converted")
print 'colorkey with RLEACCEL', t.timeit(blits)
pygame.display.flip()
converted.set_colorkey((0,255,0))
t = Timer("screen.blit(converted, (0, 100))", "from __main__ import screen, converted")
print 'nonimage colorkey no RLEACCEL', t.timeit(blits)
pygame.display.flip()
converted.set_colorkey((0,255,0), pygame.RLEACCEL)
t = Timer("screen.blit(converted, (100, 100))", "from __main__ import screen, converted")
print 'nonimage colorkey RLEACCEL', t.timeit(blits)
pygame.display.flip()
converta = image.convert_alpha()
t = Timer("screen.blit(converta, (200, 100))", "from __main__ import screen, converta")
print 'convert_alpha only', t.timeit(blits)
pygame.display.flip()
converta.set_alpha(255, pygame.RLEACCEL)
t = Timer("screen.blit(converta, (300, 100))", "from __main__ import screen, converta")
print 'convert_alpha with RLEACCEL', t.timeit(blits)
pygame.display.flip()
import time
time.sleep(2)
2) What about convert() -- this should strip alpha, if I recall
correctly. What about colorkeys? What if the colorkey isn't in the
image? Colorkeys vs. alpha is the big question in my mind.
Here are some test runs on my machine with various images from my game:
<Surface(5x22x32 SW)> 100000 blits
no optimization 0.635529994965
convert only 0.638278007507
colorkey no RLEACCEL 0.341970920563
colorkey with RLEACCEL 0.163997888565
nonimage colorkey no RLEACCEL 0.386583089828
nonimage colorkey RLEACCEL 0.156983852386
convert_alpha only 0.171095848083
convert_alpha with RLEACCEL 0.254684925079
<Surface(420x419x32 SW)> 1000 blits
no optimization 8.53273200989
convert only 0.506783008575
colorkey no RLEACCEL 3.593075037
colorkey with RLEACCEL 0.247465133667
nonimage colorkey no RLEACCEL 4.26255297661
nonimage colorkey RLEACCEL 0.309386968613
convert_alpha only 0.739364862442
convert_alpha with RLEACCEL 0.416473150253
<Surface(200x201x32 SW)> 10000 blits
no optimization 19.705960989
convert only 1.53338003159
colorkey no RLEACCEL 8.28769397736
colorkey with RLEACCEL 0.506930112839
nonimage colorkey no RLEACCEL 9.6967830658
nonimage colorkey RLEACCEL 0.57067322731
convert_alpha only 1.58401703835
convert_alpha with RLEACCEL 0.689799070358
<Surface(4x20x8 SW)> 1000000 blits
no optimization 1.31053185463
convert only 2.74165201187
colorkey no RLEACCEL 2.74318790436
colorkey with RLEACCEL 1.58007407188
nonimage colorkey no RLEACCEL 3.13079094887
nonimage colorkey RLEACCEL 1.52145981789
convert_alpha only 1.48973798752
convert_alpha with RLEACCEL 1.61767506599
<Surface(67x79x32 SW)> 100000 blits
no optimization 22.0433878899
convert only 3.54319190979
colorkey no RLEACCEL 9.50938916206
colorkey with RLEACCEL 0.883069992065
nonimage colorkey no RLEACCEL 12.828045845
nonimage colorkey RLEACCEL 1.14282393456
convert_alpha only 2.77425813675
convert_alpha with RLEACCEL 2.04481196404
<Surface(600x2289x24 SW)> 1000 blits
no optimization 9.44169402122
convert only 1.23795795441
colorkey no RLEACCEL 8.361109972
colorkey with RLEACCEL 1.09807014465
nonimage colorkey no RLEACCEL 7.3038649559
nonimage colorkey RLEACCEL 0.908895015717
convert_alpha only 1.40813684464
convert_alpha with RLEACCEL 0.938013792038
<Surface(600x2289x8 SW)> 1000 blits
no optimization 1.02722620964
convert only 1.23287391663
colorkey no RLEACCEL 7.53352308273
colorkey with RLEACCEL 0.976207971573
nonimage colorkey no RLEACCEL 7.30511307716
nonimage colorkey RLEACCEL 0.918834924698
convert_alpha only 1.42549395561
convert_alpha with RLEACCEL 0.93526506424
<Surface(402x74x32 SW)> 10000 blits
no optimization 17.0765328407
convert only 0.790960073471
colorkey no RLEACCEL 4.85476493835
colorkey with RLEACCEL 0.217236042023
nonimage colorkey no RLEACCEL 7.13477683067
nonimage colorkey RLEACCEL 0.35099196434
convert_alpha only 1.98277997971
convert_alpha with RLEACCEL 3.25428009033
-Casey