Skip to content

Commit 6ef4c0c

Browse files
committed
improve HDF5, netcdf benchmark accuracy
1 parent c8b7067 commit 6ef4c0c

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed

h5py_write_speed.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
xb = random(SIZE) > 0.5 # mean ~ 0.5
1717
xbl = xb.tolist()
1818

19-
with tempfile.NamedTemporaryFile() as fn:
19+
with tempfile.NamedTemporaryFile(suffix=".h5") as fn:
2020
with h5py.File(fn, "w") as h:
2121
tic = time()
2222
h["bool"] = xb
@@ -29,4 +29,5 @@
2929
# %% here's what nidaqmx gives us
3030
tic = time()
3131
h["listbool"] = xbl
32-
print(f"{time()-tic:3e} sec. to write boolean from bool list")
32+
# outside context manager to help ensure HDF5 file is finalized
33+
print(f"{time()-tic:3e} sec. to write boolean from bool list")

netcdf_write_speed.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from numpy import packbits
2020
import xarray
2121
from time import time
22-
import os
2322

2423

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

3231

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

39-
with tempfile.NamedTemporaryFile(suffix=".nc", delete=False) as f:
37+
with tempfile.NamedTemporaryFile(suffix=".nc") as f:
4038
tic = time()
4139
xi = packbits(xbl, axis=0) # each column becomes uint8 BIG-ENDIAN
4240
Xi = xarray.DataArray(xi, name="uint8")
4341
Xi.to_netcdf(f.name, "w", engine="netcdf4")
44-
os.unlink(f.name)
4542
print(f"{time()-tic:.3f} sec. to write uint8")
4643
# %% here's what nidaqmx gives us
47-
with tempfile.NamedTemporaryFile(suffix=".nc", delete=False) as f:
44+
with tempfile.NamedTemporaryFile(suffix=".nc") as f:
4845
tic = time()
4946
Xbl = xarray.DataArray(xbl, name="listbool")
5047
Xbl.to_netcdf(f.name, "w")
51-
os.unlink(f.name)
5248
print(f"{time()-tic:.3f} sec. to write boolean from bool list")

0 commit comments

Comments
 (0)