Skip to content

Instantly share code, notes, and snippets.

@swarbhanu
Created November 29, 2011 15:00
Show Gist options
  • Save swarbhanu/1405089 to your computer and use it in GitHub Desktop.
Save swarbhanu/1405089 to your computer and use it in GitHub Desktop.
This program measures the time needed to read an array from an hdf file and save it in a numpy array using a slow asarray() method and a fast method with the dtype provided. Run the program as $ python timeNumpyArrayConversion.py <tempfilename.h5> 100 100
#################################################################################
# #
# This program measures the time needed to read an array from an hdf file and #
# save it in a numpy array using a slow asarray() method and a fast method with #
# the dtype explicitly provided before construction. The fast method is to be #
# preferred for converting arrays read from hdf files using PyTables to Numpy #
# arrays. #
# #
# This file is meant to be run as in the following example: #
# #
# $ python timeNumpyArrayConversion.py <tempfilename.h5> <xdim> <ydim> #
# $ python timeNumpyArrayConversion.py myTempfile.h5 1000 1000 #
# #
#################################################################################
import sys, time, h5py, numpy
filename=sys.argv[1] # read in the filename that will be created for the test
x=float(sys.argv[2]) # the x dimension of the array to be created
y=float(sys.argv[3]) # the y dimension
shape=(x,y)
print "The shape of the array is ", shape
print ""
#open an hdf5 file
f = h5py.File(filename,'w')
grp = f.create_group('grp1')
#create a dataset
datasetWrite = grp.create_dataset('datasetWrite',shape,'=f8',maxshape=(None,None))
f['datasetWrite']=numpy.array(numpy.arange(x*y)).reshape(shape)
#close the file
f.close()
#########################################################################################
# Checking time to read an array from an hdf5 file and converting it into a numpy array #
#########################################################################################
def slow_conversion(hdfarray):
return numpy.array(hdfarray)
def fast_conversion(hdfarray):
a=numpy.empty(shape=hdfarray.shape,dtype=hdfarray.dtype)
a[:]=hdfarray[:]
return a
# slow conversion from a pytables array to a numpy array
t1=time.time()
hf=h5py.File(filename,'r')
numpyArray_Slow = slow_conversion(f['datasetWrite'])
print "Slow conversion time: %8.4f s" %(time.time() - t1)
hf.close()
# fast conversion from a pytables array to a numpy array
t2=time.time()
hf=h5py.File(filename,'r')
numpyArray_Fast = fast_conversion(f['datasetWrite'])
print "Fast conversion time: %8.4f s" %(time.time() - t2)
hf.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment