-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_chunked.py
51 lines (41 loc) · 1.3 KB
/
benchmark_chunked.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import os
import sys
import timeit
import h5py
import numpy as np
n, k, l = 100000, 50, 128
def create(filename):
"""Create a contiguous array of size (n,k,l)."""
with h5py.File(filename, "w") as f:
print("Creating HDF5 file...")
a = f.create_dataset('/test',
dtype=np.int16,
shape=(n,k,l),
chunks=(100,k,l))
n_ = n//10
for i in range(10):
print i,
a[i*n_:(i+1)*n_,...] = np.random.randint(size=(n_,k,l),
low=-32000,
high=32000)
filename = 'test_chk.h5'
if 0 or not os.path.exists(filename):
create(filename)
size = 1000
# ind = np.arange(0, n, n//size)+0
ind = np.random.randint(size=(size), low=0, high=n)
ind = np.unique(ind)
def read(a, out):
for j, i in enumerate(ind):
out[j:j+1,...] = a[i:i+1,...]
return out
with h5py.File(filename, "r") as f:
a = f['/test']
out = np.empty((size,k,l), dtype=a.dtype)
t0 = timeit.default_timer()
read(a, out)
t1 = timeit.default_timer()
d = t1-t0
bandwidth = size*k*l*2/(1024*1024.)/d
print("Elapsed: {0:.2f} s".format(d))
print("Bandwidth: {0:.1f} MB/s".format(bandwidth))