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 51 52 53 54 55
|
#!/usr/bin/env python
from time import time
import numpy as np
import tables as tb
# Size of the buffer to be appended
M = 1_000_000
# Number of rows in table
N = 100 * M
filename = "simple-table-blosc2.h5"
filters = tb.Filters(9, "blosc2", shuffle=True)
# filters = tb.Filters(9, "zlib", shuffle=True)
# dt = np.dtype([('int32', np.int32), ('float32', np.float32, 10)])
dt = np.dtype([("int32", np.int32), ("float32", np.float32)])
a = np.fromiter(((i, i) for i in range(M)), dtype=dt)
# a = np.zeros(M, dtype=dt)
if 1:
output_file = tb.open_file(filename, mode="w", PYTABLES_SYS_ATTRS=False)
table = output_file.create_table(
"/", "test", dt, filters=filters, expectedrows=N
) # , chunkshape=131072)
chunkshape = table.chunkshape[0]
print("chunkshape:", chunkshape)
t0 = time()
for i in range(0, N, M):
table.append(a)
# table.append(a[1:-1])
table.flush()
print(f"Time for storing: {time() - t0 : .3f}s")
output_file.close()
print("Start reads:")
output_file = tb.open_file(filename)
table = output_file.root.test
t0 = time()
result = 0
# for i in range(0, N, 8192):
# result += table[i]['int32']
# for row in table:
# result += row['int32']
b = table[:]
print(f"Time for reading: {time() - t0 : .3f}s")
print("result:", result)
output_file.close()
|