I'm trying to figure out why this happens:
In [1]: import time, h5py as h5
In [2]: f = h5.File('myfile.hdf5', 'r')
In [3]: st = time.time(); data = f["data"].value[0,:,1,...]; elapsed = time.time() - st;
In [4]: elapsed
Out[4]: 11.127676010131836
In [5]: st = time.time(); data = f["data"][0,:,1,...]; elapsed2 = time.time() - st;
In [6]: elapsed2
Out[6]: 59.810582399368286
In [7]: f["data"].shape
Out[7]: (1, 4096, 6, 16, 16, 16, 16)
In [8]: f["data"].chunks
Out[8]: (1, 4096, 1, 16, 16, 16, 16)
As you can see, loading the whole dataset into memory and then taking a slice is faster than taking that same slice from the dataset.
The chunk size matches the slice, so it should all be contiguous memory, right? Why then is it so much slower?
The dataset is compressed with gzip (opts=2
).
Following Andrew's comment, I run it clearing the caches between both reads:
elapsed1: 11.001180410385132
elapsed2: 43.19723725318909
48.61user 4.45system 0:54.65elapsed 97%CPU (0avgtext+0avgdata 8431596maxresident)k
479584inputs+0outputs (106major+3764414minor)pagefaults 0swaps
(This next run had a 10 second delay between the two reads to clear the caches)
elapsed1: 11.46790862083435
elapsed2: 43.438515186309814
48.54user 4.66system 1:05.71elapsed 80%CPU (0avgtext+0avgdata 8431944maxresident)k
732504inputs+0outputs (220major+3764449minor)pagefaults 0swaps