Hi,
I've just started using wxPython. Working on a cellular automaton I did
some timings and got this (just the top):
···
--------------------------------------------------------------------------------
Wed Apr 23 22:22:11 2008 caprofile7
1943247 function calls in 14.565 CPU seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
1 3.544 3.544 14.169 14.169
/usr/lib/python2.5/site-packages/wx-2.8-gtk2-unicode/wx/_core.py:7219(MainLoop)
158760 2.855 0.000 10.134 0.000
/home/folkert/workarea/cellularautomaton/dharana/wxCA/canumclass3.py:56(parity)
120393 2.081 0.000 5.379 0.000
/home/folkert/workarea/cellularautomaton/dharana/wxCA/canumclass3.py:259(setADeadPixel)
162181 1.311 0.000 1.948 0.000
/usr/lib/python2.5/site-packages/wx-2.8-gtk2-unicode/wx/_gdi.py:982(__init__)
162181 0.888 0.000 1.168 0.000
/usr/lib/python2.5/site-packages/wx-2.8-gtk2-unicode/wx/_gdi.py:1047(__init__)
41788 0.723 0.000 1.785 0.000
/home/folkert/workarea/cellularautomaton/dharana/wxCA/canumclass3.py:271(setALifePixel)
162181 0.536 0.000 0.536 0.000
/usr/lib/python2.5/site-packages/wx-2.8-gtk2-unicode/wx/_gdi.py:1080(MoveTo)
10 0.426 0.043 10.560 1.056
/home/folkert/workarea/cellularautomaton/dharana/wxCA/canumclass3.py:343(applyRule)
--------------------------------------------------------------------------------
My question is if there is a way to optimize things? I can't do anything
about the "MainLoop" time I guess?
For the "parity" and "setADeadPixel" functions the code is included below.
Is the bitmap access in "SetADeadPixel" the only and most efficient way
to manipulate pixel values?
What is the difference in using pda.Offset(...) vs pda.MoveTo(...)?
I couldn't find a direct way to retrieve pixel values directly so shadow
values are kept in a byte array img['b'].
This allows for direct comparison of pixel values (RGB and Alpha info).
Thanks in advance,
Folkert
--------------------------------------------------------------------------------
def parity(self, x, y, img):
sum = 0
if img['b'][x,y-1] == lifeCell: # comparing 2 32-bit values
sum += 1
if img['b'][x-1,y] == lifeCell:
sum += 1
if img['b'][x,y] == lifeCell:
sum += 1
if img['b'][x+1,y] == lifeCell:
sum += 1
if img['b'][x,y+1] == lifeCell:
sum += 1
if sum == 1:
self.setLife(img, x, y)
return 1
else:
self.setDead(img, x, y) # actually a call to
setADeadPixel
return 0
-------------------------------------------------------------------------------
def setADeadPixel(self, img, x, y):
# Set the pixel in the bitmap:
pd = wx.AlphaPixelData(img['i'])
if not pd:
raise RuntimeError("Failed to gain raw access to bitmap data.")
pda = wx.AlphaPixelData_Accessor(img['i'], pd)
#pda.Offset(pd, x, y)
pda.MoveTo(pd, x, y)
pda.Set(DCR, DCG, DCB, DCA)
# Set the pixel in the buffer:
img['b'][x,y] = deadCell
-----------------------------------------------------------------------------------------------------------------