Storing numpy arrays in hdf5 files using h5py is great, because you can load parts of the array from disk. One thing to note is that there is a varying amount of time overhead depending on the kind of indexing you use.
It turns out that it is fastest to use standard python slicing terminology - [:20,:] - which grabs well defined contiguous sections of the array.
If we use an array of consecutive numbers as an index we get an additional time overhead simply for using this kind of index.
If we use an array of non-consecutive numbers (note that the indecies have to be monotonic and non-repeating) we get yet another time overhead even above the array with consecutive indexes.
Just something to keep in mind when implementing algorithms.
It turns out that it is fastest to use standard python slicing terminology - [:20,:] - which grabs well defined contiguous sections of the array.
If we use an array of consecutive numbers as an index we get an additional time overhead simply for using this kind of index.
If we use an array of non-consecutive numbers (note that the indecies have to be monotonic and non-repeating) we get yet another time overhead even above the array with consecutive indexes.
Just something to keep in mind when implementing algorithms.
import numpy, h5py N = 1000 m = 50 f = h5py.File('index_test.h5','w') f.create_dataset('data', data=numpy.random.randn(N,1000)) idx1 = numpy.array(range(m)) idx2 = numpy.array(range(N-m,N)) idx3 = numpy.random.choice(N,size=m,replace=False) idx3.sort() timeit f['data'][:m,:] timeit f['data'][-m:,:] timeit f['data'][idx1,:] timeit f['data'][idx2,:] timeit f['data'][idx3,:] f.close() # N = 1000 #-> 1000 loops, best of 3: 279 µs per loop #-> 1000 loops, best of 3: 281 µs per loop #-> 1000 loops, best of 3: 888 µs per loop #-> 1000 loops, best of 3: 891 µs per loop #-> 1000 loops, best of 3: 1.27 ms per loop # N = 10000 #-> 1000 loops, best of 3: 258 µs per loop #-> 1000 loops, best of 3: 258 µs per loop #-> 1000 loops, best of 3: 893 µs per loop #-> 1000 loops, best of 3: 892 µs per loop #-> 1000 loops, best of 3: 1.3 ms per loop
Comments
Post a Comment