Hello All,
I'm pretty new to python but have been programming, for a few years hobby wise and decided to try python after I found how concise the syntax was and with a computer science bent. So I'm writing a little code for a work related thing, simulating some basic message passing model we're working with. I finally got something to work and it's pretty shocking how long it takes. To fill a 10x20 array it must take 30-40 minutes. I can do it by hand faster and was hoping to fill arrays of thousands.
Anyway any help appreciated, I realise that most of it's loops and comparisons which a brief look over the python optimisation guide tells me is really inefficient but I can't see anyway of using different methods to accomplish this.
#! /usr/bin/env python #Define size of matrix xSize, ySize = 10, 20
#Define sources s1x, s1y = 0, 0 s2x, s2y = 1, 1
# create a 10 x 10 matrix of zeroes medium = [['e' for y in range(ySize)] for x in range(xSize)]
#Return the tuple of points that is the moore neighbourhood def moorePoints(tup): coords = () for x1 in range(-1,2): for y1 in range(-1,2): coords += (tup[0]+x1,tup[1]+y1), return coords #Print the array def displayMed(): for row in reversed(medium): print row #return coords not already in the array def newCoords(tup): newtup = () for k in tup: if k[0] >= xSize or k[1] >= ySize or k[0] < 0 or k[1] < 0: pass elif medium[k[0]][k[1]] == 'e': newtup += k, return newtup
#Add the coordinates to the array def addCoords(tup, time): for k in tup: medium[k[0]][k[1]] = time
#Calculate maximum number of iterations required loop = max(abs(((xSize/2)-s2x))+(xSize/2),abs(((ySize/2)-s2y))+(ySize/2)) setOfPoints = (s2x,s2y), medium[s2x][s2y] = 0 print 'loops', loop
#MainLoop
for j in range(loop): print 'loop:', j newPoints = () for k in setOfPoints: newPoints += moorePoints(k) addCoords(newCoords(newPoints),(j+1)) setOfPoints = newPoints #print setOfPoints, '::::', newPoints displayMed() print '' displayMed()
[\code][code=python]
#! /usr/bin/env python
#Define size of matrix
xSize, ySize = 10, 20
#Define sources
s1x, s1y = 0, 0
s2x, s2y = 1, 1
# create a 10 x 10 matrix of zeroes
medium = [ for x in range(xSize)]
#Return the tuple of points that is the moore neighbourhood
def moorePoints(tup):
coords = ()
for x1 in range(-1,2):
for y1 in range(-1,2):
coords += (tup[0]+x1,tup[1]+y1),
return coords
#Print the array
def displayMed():
for row in reversed(medium):
print row
#return coords not already in the array
def newCoords(tup):
newtup = ()
for k in tup:
if k[0] >= xSize or k[1] >= ySize or k[0] < 0 or k[1] < 0:
pass
elif medium[k[0]][k[1]] == 'e':
newtup += k,
return newtup
#Add the coordinates to the array
def addCoords(tup, time):
for k in tup:
medium[k[0]][k[1]] = time
#Calculate maximum number of iterations required
loop = max(abs(((xSize/2)-s2x))+(xSize/2),abs(((ySize/2)-s2y))+(ySize/2))
setOfPoints = (s2x,s2y),
medium[s2x][s2y] = 0
print 'loops', loop
#MainLoop
for j in range(loop):
print 'loop:', j
newPoints = ()
for k in setOfPoints:
newPoints += moorePoints(k)
addCoords(newCoords(newPoints),(j+1))
setOfPoints = newPoints
#print setOfPoints, '::::', newPoints
displayMed()
print ''
displayMed()
[\code]