Calculating mesh centers runs very slowly I have a piece of code that is used to calculate the cell centers in a rectangular mesh. The mesh size is pretty large (650 columns by 1150 rows). The code runs very slowly. I was wondering if I could employ any tricks to reduce the runtime.

In the example below, deltax is a 1D array of column spacings and deltay is a 1D array of row spacings. Maybe I need to do some sort of vectorization.

center_x = np.empty((nrow,ncol),dtype ='double')
center_y = np.empty((nrow,ncol),dtype ='double')
for i in range(nrow-1,-1,-1):
    if(i == nrow-1):
        center_y[i,:] = 0.5*deltay[nrow-1]
    else:
        center_y[i,:] = center_y[i+1,:] + 0.5*deltay[i+1] + 0.5*deltay[i]

for j in range(0,ncol):
    if(j ==0):
        center_x[:,j] = 0.5*deltax[0]
    else:
        center_x[:,j]= center_x[0,j-1] + 0.5*deltax[j-1] + 0.5*deltax[j]
There are two things I would try to change:


Is it necessary, that center_x and center_y are 2-dimensional arrays? In your code, the 2nd dimension holds redundant information and should be avoided.
The biggest problem with pure Python (but often the greatest benefit ;) ) is that it is an interpreted language and numerically heavy operations should be handled with library functions. In your case, I would try to exploit numpy's array features and calculate the center as

center_x = 0.5 * (delta_x[:ncol-1] + delta_x[1:])