Skip to content

Commit

Permalink
improve HDF5, netcdf benchmark accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Apr 16, 2024
1 parent c8b7067 commit 6ef4c0c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
5 changes: 3 additions & 2 deletions h5py_write_speed.py
Expand Up @@ -16,7 +16,7 @@
xb = random(SIZE) > 0.5 # mean ~ 0.5
xbl = xb.tolist()

with tempfile.NamedTemporaryFile() as fn:
with tempfile.NamedTemporaryFile(suffix=".h5") as fn:
with h5py.File(fn, "w") as h:
tic = time()
h["bool"] = xb
Expand All @@ -29,4 +29,5 @@
# %% here's what nidaqmx gives us
tic = time()
h["listbool"] = xbl
print(f"{time()-tic:3e} sec. to write boolean from bool list")
# outside context manager to help ensure HDF5 file is finalized
print(f"{time()-tic:3e} sec. to write boolean from bool list")
10 changes: 3 additions & 7 deletions netcdf_write_speed.py
Expand Up @@ -19,7 +19,6 @@
from numpy import packbits
import xarray
from time import time
import os


SIZE = (3, 200000) # arbitrary size to test
Expand All @@ -30,23 +29,20 @@
Xb = xarray.DataArray(xb, name="bool")


with tempfile.NamedTemporaryFile(suffix=".nc", delete=False) as f:
with tempfile.NamedTemporaryFile(suffix=".nc") as f:
tic = time()
Xb.to_netcdf(f.name, "w")
os.unlink(f.name)
print(f"{time()-tic:.3f} sec. to write boolean from Numpy bool")

with tempfile.NamedTemporaryFile(suffix=".nc", delete=False) as f:
with tempfile.NamedTemporaryFile(suffix=".nc") as f:
tic = time()
xi = packbits(xbl, axis=0) # each column becomes uint8 BIG-ENDIAN
Xi = xarray.DataArray(xi, name="uint8")
Xi.to_netcdf(f.name, "w", engine="netcdf4")
os.unlink(f.name)
print(f"{time()-tic:.3f} sec. to write uint8")
# %% here's what nidaqmx gives us
with tempfile.NamedTemporaryFile(suffix=".nc", delete=False) as f:
with tempfile.NamedTemporaryFile(suffix=".nc") as f:
tic = time()
Xbl = xarray.DataArray(xbl, name="listbool")
Xbl.to_netcdf(f.name, "w")
os.unlink(f.name)
print(f"{time()-tic:.3f} sec. to write boolean from bool list")

0 comments on commit 6ef4c0c

Please sign in to comment.