|
| 1 | +####################################################################### |
| 2 | +# Copyright (c) 2019-present, Blosc Development Team <[email protected]> |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under a BSD-style license (found in the |
| 6 | +# LICENSE file in the root directory of this source tree) |
| 7 | +####################################################################### |
| 8 | + |
| 9 | +# This compares performance of creating and reading a NumPy array in different ways: |
| 10 | +# 1) memory |
| 11 | +# 2) disk |
| 12 | +# 3) disk with b2zip format |
| 13 | + |
| 14 | +import blosc2 |
| 15 | + |
| 16 | +from time import time |
| 17 | + |
| 18 | +# Number of elements in array |
| 19 | +N = 2**27 |
| 20 | + |
| 21 | +def b2_native(urlpath=None): |
| 22 | + t0 = time() |
| 23 | + a = blosc2.linspace(0., 1., N, urlpath=urlpath, mode="w") |
| 24 | + # a = blosc2.linspace(0., 1., 2**27, cparams=blosc2.CParams(codec=blosc2.Codec.LZ4)) |
| 25 | + # a = blosc2.linspace(0., 1., 2**27, dparams=blosc2.DParams(nthreads=1)) |
| 26 | + t1 = time() |
| 27 | + print(f"Time to create a linspace array: {t1 - t0:.2f}s, bandwidth: {a.nbytes / (t1 - t0) / 1e9:.2f} GB/s") |
| 28 | + #print(a.info) |
| 29 | + |
| 30 | + t0 = time() |
| 31 | + b = a[:] |
| 32 | + t1 = time() |
| 33 | + print(f"Time to read the array: {t1 - t0:.2f}s, bandwidth: {b.nbytes / (t1 - t0) / 1e9:.2f} GB/s") |
| 34 | + |
| 35 | +def b2_b2zip(urlpath): |
| 36 | + t0 = time() |
| 37 | + with blosc2.TreeStore(localpath=urlpath, mode="w") as tstore: |
| 38 | + a = blosc2.linspace(0., 1., N) |
| 39 | + # a = blosc2.linspace(0., 1., 2**27, cparams=blosc2.CParams(codec=blosc2.Codec.LZ4)) |
| 40 | + tstore["/b"] = a |
| 41 | + t1 = time() |
| 42 | + print(f"Time to store a linspace array: {t1 - t0:.2f}s, bandwidth: {a.nbytes / (t1 - t0) / 1e9:.2f} GB/s") |
| 43 | + |
| 44 | + t0 = time() |
| 45 | + with blosc2.TreeStore(localpath=urlpath, mode="r") as tstore_read: |
| 46 | + b = tstore_read["/b"][:] |
| 47 | + t1 = time() |
| 48 | + print(f"Time to read the array: {t1 - t0:.2f}s, bandwidth: {b.nbytes / (t1 - t0) / 1e9:.2f} GB/s") |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + print("Blosc2 in-memory") |
| 53 | + b2_native() |
| 54 | + print("Blosc2 on disk") |
| 55 | + b2_native("linspace.b2nd") |
| 56 | + print("Blosc2 on disk with b2zip format") |
| 57 | + b2_b2zip("my_tstore.b2z") |
0 commit comments