import patch

import numpy as np
import time

myg = patch.Grid2d(512,1024, xmax=1.0, ymax=2.0)

myd = patch.CellCenterData2d(myg)

bc = patch.BCObject()
myd.register_var("a", bc)
myd.create()

a = myd.get_var("a")
a[:,:] = np.random.rand(myg.qx, myg.qy)


# slicing method
start = time.time()

da = myg.scratch_array()
da[myg.ilo:myg.ihi+1,myg.jlo:myg.jhi+1] = \
    a[myg.ilo+1:myg.ihi+2,myg.jlo:myg.jhi+1] - \
    a[myg.ilo-1:myg.ihi  ,myg.jlo:myg.jhi+1]

print "slice method: ", time.time() - start

                            
# mask method
m = patch.Mask(myg.nx, myg.ny, myg.ng)

start = time.time()
da2 = myg.scratch_array()
da2[m.valid] = a[m.ip1] - a[m.im1]

print "mask method: ", time.time() - start

print np.max(np.abs(da2 - da))

@@ -39,16 +39,25 @@
 t time%0A%0A
+n = 512%0A%0A
 myg = pa
@@ -71,16 +71,13 @@
 d2d(
-512,1024
+n,2*n
 , xm
@@ -253,17 +253,75 @@
 myg.qy)%0A
+#a%5B:,:%5D = np.arange(myg.qx*myg.qy).reshape(myg.qx, myg.qy)
 %0A
-
 %0A# slici
@@ -769,16 +769,16 @@
  start%0A%0A
-
 print np
@@ -800,8 +800,271 @@
  - da))%0A
+%0A%0A# roll -- note, we roll in the opposite direction of the shift%0Astart = time.time()%0Ada3 = myg.scratch_array()%0Ada3%5B:%5D = np.roll(a, -1, axis=0) - np.roll(a, 1, axis=0)%0A%0Aprint %22roll method: %22, time.time() - start%0A%0Aprint np.max(np.abs(da3%5Bm.valid%5D - da%5Bm.valid%5D))%0A%0A%0A

add a roll() comparison