import numpy
import tables
from time import time

shape = (20, 4, 0, 3)
nrows = 15678
niter = 1000

def createEArrays(n):
    chunkshape = list(shape)
    chunkshape[2] = n
    e = f.createEArray('/', 'e%d'%n, tables.FloatAtom(),
                       shape=shape, chunkshape=chunkshape,
                       filters=tables.Filters(complevel=1, complib='zlib'))
    nelemchunk = numpy.prod(chunkshape)
    for i in xrange(nrows/n):
        e.append(numpy.arange(nelemchunk).reshape(chunkshape))

print "************** Writes ************"
f = tables.openFile("/tmp/p.h5", "w")
for n in (1, 5, 10):
    t1 = time()
    createEArrays(n)
    print "e%d. Time took for writing: %s" % (n, round(time()-t1, 3))
f.close()

print "************** Reads *************"
# Time sparse reads
f = tables.openFile("/tmp/p.h5")
for n in (1,5,10):
    e = f.getNode('/', 'e%d'%n)
    t1 = time()
    for offset in numpy.random.randint(0, e.nrows, niter):
        r = e[:,:,offset,:]
    print "e%d. Time took for %s reads: %s" % (n, niter, round(time()-t1, 3))
f.close()
