I'm a visual artist who is learning Python in order to create a specify set of artworks. In one, I'm coding Conway's classic Game of Life using a 2160x3840 grid.
However, the program is running slower than I'd hoped: It's been running on my three-year old iMac now for 24 hours and I only have two and a half "frames" processed. It will take weeks to make the run, and I have several runs to do.
I ran SnakeViz and 93% of my program's time is spent in a single function, where the primary activity is a series of comparisons. All of the "finch" colors and flashOfLifeColor are considered "live" cells in terms of Conway's rules.
def isLive (theColor):
isCellLive = False
finchColor_1 = numpy.array([247, 238, 214])
finchColor_2 = numpy.array([202, 184, 88])
finchColor_3 = numpy.array([103, 81, 68])
flashOfLifeColor = numpy.array([249, 192, 0])
if (numpy.array_equal(theColor, finchColor_1)) or
(numpy.array_equal(theColor, finchColor_2)) or
(numpy.array_equal(theColor, finchColor_3)) or
(numpy.array_equal(theColor, flashOfLifeColor)):
isCellLive = True
return isCellLive
Is there a better (faster) way to write the if statement? Is there anything else I can do outside of optimization to speed things up?
Thanks,
--Darin
Edit:
Here is the function that is calling isLive to help understand what I am doing. I also want to mention again that I'm very new at Python programming, and do not yet know object programming at all, not any advanced techniques--thsu having a hard time deciphering some of the implementations of Conway's Rules that I am seeing on the web.
def countNeighborsNine(theArray, row, column):
numberOfNeighbors = 0
maxRow, maxColumn, depth = theArray.shape
for rowModifier in range (-1,2):
for columnModifier in range (-1, 2):
rowNeighborPointer = (row + rowModifier) % maxRow
columnNeighborPointer = (column + columnModifier) % maxColumn
thePixel = theArray[rowNeighborPointer, columnNeighborPointer]
if isLive(thePixel):
numberOfNeighbors = numberOfNeighbors + 1
return numberOfNeighbors

isLivefunction?image, and you want to get a list of alllivecells? And istheColoris always a 3-element array?