Skip to content

Instantly share code, notes, and snippets.

@upupming
Created December 29, 2021 10:42
Show Gist options
  • Save upupming/25034f998ba8ab8bd65590a1db40e8dd to your computer and use it in GitHub Desktop.
Save upupming/25034f998ba8ab8bd65590a1db40e8dd to your computer and use it in GitHub Desktop.
# Test saving speed of different data format
import numpy as np
import pickle
import nibabel as nib
import time
from os import path
import SimpleITK as sitk
a = np.empty((512, 512, 512))
tik1 = time.time()
with open(path.join(path.dirname(__file__), 'test.pkl'), 'wb') as f:
pickle.dump(a, f)
tik2 = time.time()
print(f'pkl saved in {tik2 - tik1}s')
b = sitk.GetImageFromArray(a)
sitk.WriteImage(b, path.join(path.dirname(__file__), 'test.nii.gz'))
tik3 = time.time()
print(f'nii saved in {tik3 - tik2}s')
np.save(path.join(path.dirname(__file__), 'test.npy'), a)
tik4 = time.time()
print(f'npy saved in {tik4 - tik3}s')
np.savez_compressed(path.join(path.dirname(__file__), 'test.npz'), a)
tik5 = time.time()
print(f'npz saved in {tik5 - tik4}s')
# pkl saved in 4.438295602798462s
# nii saved in 7.4495849609375s
# npy saved in 3.920564651489258s
# npz saved in 7.050110816955566s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment