[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Numpy slower than Numeric
Jakub Piotr CÅapa wrote:
Kamilche wrote:
I make heavy use of Numeric in my sprite engine.
When I did the following, I was able to 'drop in' numpy as a
replacement, but it took 3x longer to load my complex graphics! :-O
Maybe you could offer a reduced test case so we could check this? I'm
sure numpy developers would also be interested...
Sure, here it is. It's a total hack using global variables, but it
works. Numeric takes 1 second, numpy takes 2.6, doing the same
operations to the same picture.
--Kamilche
import Numeric
import numpy
import urllib
import pygame
import os
import cStringIO
import time
bg = None
screen = None
pic = None
pic2 = None
def TestNumeric():
global pic2
pic2 = GetPic()
array = pygame.surfarray.array3d(pic2).astype(Numeric.Int)
alphaarray = pygame.surfarray.array_alpha(pic).astype(Numeric.UInt8)
starttime = time.time()
for i in range(10):
array[:, :] = Numeric.clip(array + [20, 0, 0], 0, 255)
pygame.surfarray.blit_array(pic2, array)
pygame.surfarray.pixels_alpha(pic2)[:, :] = alphaarray
Update()
print 'Numeric time: %f seconds' % (time.time() - starttime)
def TestNumpy():
global pic2
pic2 = GetPic()
array = pygame.surfarray.array3d(pic2).astype(numpy.int)
alphaarray =
pygame.surfarray.array_alpha(pic).astype(numpy.unsignedinteger)
starttime = time.time()
for i in range(10):
array[:, :] = numpy.clip(array + [20, 0, 0], 0, 255)
pygame.surfarray.blit_array(pic2, array)
pygame.surfarray.pixels_alpha(pic2)[:, :] = alphaarray
Update()
print 'numpy time: %f seconds' % (time.time() - starttime)
def GetPic(url = 'http://www.kamilche.com/images/pygame/elf.png'):
filename = 'elf.tga'
if os.path.exists(filename):
return pygame.image.load(filename)
print 'Downloading %s' % url
fd = urllib.urlopen(url)
picdata = fd.read()
fd.close()
pic = pygame.image.load(cStringIO.StringIO(picdata))
pygame.image.save(pic, filename)
return pic
def Update():
bg.fill((64, 128, 32, 128))
bg.blit(pic, (0, 0))
bg.blit(pic2, (pic.get_width()+10, 0))
screen.blit(bg, (0, 0))
pygame.display.update()
def main():
global bg, screen, pic, pic2
pygame.init()
screen = pygame.display.set_mode((800, 600), 0, 32)
pic = GetPic()
pic2 = GetPic()
bg = pygame.Surface((800, 600), pygame.SRCALPHA, 32).convert_alpha()
Update()
TestNumeric()
TestNumpy()
main()