[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Numpy slower than Numeric
Rikard Bosnjakovic wrote:
On 12/28/06, Kamilche <kamilche@xxxxxxxxxxxx> wrote:
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.
On line 30, array = pygame.surfarray.array3d(pic2).astype(numpy.int),
I need to change the final ".int" to ".Int", else it won't work for me
("AttributeError: 'module' object has no attribute 'int'"). After
switching, I get these results:
Downloading http://www.kamilche.com/images/pygame/elf.png
Numeric time: 0.437000 seconds
numpy time: 0.609000 seconds
If I run it 10 times in a row, the last result it this:
Numeric time: 0.453000 seconds
numpy time: 0.578000 seconds
That is, I cannot reproduce the slow behaviour on your machine. Unless
it's because of the int -> Int-change, ofcourse.
Ok, version check - if I change it back to Int:I get an error
'AttributeError: 'module' object has no attribute 'Int'' error, the
exact opposite of yours.
Numeric version: 24.2
Numeric time: 0.954000 seconds
numpy version: 1.0.1
numpy time: 2.312000 seconds
I modified the code to print the versions.
Here's the modified code:
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()
print 'Numeric version: %s' % Numeric.__version__
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()
print 'numpy version: %s' % numpy.__version__
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()