The following:
for x in range(rows):
for y in range(cols):
if Z[x][y] == 1:
if (N[x][y] < 2) or (N[x][y] > 3):
Z[x][y] = 0
else:
if (N[x][y] == 3):
Z[x][y] = 1
could be replaced by:
set_zero_idxs = (Z==1) & ((N<2) | (N>3))
set_one_idxs = (Z!=1) & (N==3)
Z[set_zero_idxs] = 0
Z[set_one_idxs] = 1
This will take more operations than the loop that you have, but I would expect it to be faster.
EDIT:
So I have just benchmarked the two solutions, somewhat unsurprisingly the numpy version is 180 times faster:
In [49]: %timeit no_loop(z,n)
1000 loops, best of 3: 177 us per loop
In [50]: %timeit loop(z,n)
10 loops, best of 3: 31.2 ms per loop
EDIT2:
I think that this loop:
for x in range(rows):
for y in range(cols):
Q = [q for q in [x-1, x, x+1] if ((q >= 0) and (q < cols))]
R = [r for r in [y-1, y, y+1] if ((r >= 0) and (r < rows))]
S = [Z[q][r] for q in Q for r in R if (q, r) != (x, y)]
N[x][y] = sum(S)
can be replaced by:
N = np.roll(Z,1,axis=1) + np.roll(Z,-1,axis=1) + np.roll(Z,1,axis=0) + np.roll(Z,-1,axis=0)
Here there is an implicit assumption that the array does not have bounds and that x[-1]
is next to x[0]
. If this is a problem, you could add a buffer of zeros around your array with:
shape = Z.shape
new_shape = (shape[0]+2,shape[1]+2)
b_z = np.zeros(new_shape)
b_z[1:-1,1:-1] = Z
b_n = np.roll(b_z,1,axis=1) + np.roll(b_z,-1,axis=1) + np.roll(b_z,1,axis=0) + np.roll(b_z,-1,axis=0)
N = b_n[1:-1,1:-1]
And for a benchmark:
In [4]: %timeit computeNeighbours(z)
10 loops, best of 3: 140 ms per loop
In [5]: %timeit noloop_computeNeighbours(z)
10000 loops, best of 3: 133 us per loop
In [6]: %timeit noloop_with_buffer_computeNeighbours(z)
10000 loops, best of 3: 170 us per loop
So just a small improvement of a factor of 1052. Hooray for Numpy!