pip install h5py
import h5py
import numpy as np
xx = np.random.random(1000000)
yy = np.random.random([1000,1000])
h5f = h5py.File('hy.h5','w')
h5f.create_dataset('x',data=xx)
h5f.create_dataset('y',data=yy)
h5f.close()
with h5py.File('hy.h5','w') as h5f:
h5f.create_dataset('x',data=xx)
h5f.create_dataset('y',data=yy)
h5f = h5py.File('hy.h5')
xx = h5f['x'][:]
yy = h5f['y'][:]
h5f.close()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import time,h5py,os
from astropy.io import fits
chuefile = 'xy'
def saveh5f(x,y,n):
h5f = h5py.File(chuefile+'%d.h5'%n,'w')
h5f.create_dataset('x',data=x)
h5f.create_dataset('y',data=y)
h5f.close()
def savenpy(x,y,n):
np.save(chuefile+'_x%d.npy'%n,x)
np.save(chuefile+'_y%d.npy'%n,y)
def savenpz(x,y,n):
np.savez(chuefile+'%d.npz'%n,x=x,y=y)
def savefits(x,y,n):
if(os.path.isfile(chuefile+'%d.fits'%n)): os.remove(chuefile+'%d.fits'%n)
fits.HDUList([fits.PrimaryHDU(x),fits.ImageHDU(y)]).writeto(chuefile+'%d.fits'%n)
for f in [saveh5f,savenpy,savenpz,savefits]:
ttt = []
ttt_std = []
nnn = 10**np.arange(3,9)
for n in nnn:
x = np.random.random(n)
y = np.random.random(int(n/10))
tt = []
for i in range(5):
t1 = time.time()
f(x,y,n)
tt.append(time.time()-t1)
ttt.append(tt)
ttt = np.log10(np.array(ttt))
std = np.array(ttt).std(1)
mean = np.array(ttt).mean(1)
nnn = np.log10(nnn)
plt.plot(nnn,mean)
plt.fill_between(nnn,mean-std,mean+std,alpha=0.3)
plt.xlabel('log(จำนวนข้อมูล)',family='Tahoma')
plt.ylabel('log(เวลา)',family='Tahoma')
plt.legend(['h5','npy','npz','fits'])
plt.show()
def loadh5f(n):
with h5py.File(chuefile+'%d.h5'%n,'r') as h5f:
x = h5f['x'][:]
y = h5f['y'][:]
return x,y
def loadnpy(n):
x = np.load(chuefile+'_x%d.npy'%n)
y = np.load(chuefile+'_y%d.npy'%n)
return x,y
def loadnpz(n):
xy = np.load(chuefile+'%d.npz'%n)
return xy['x'],xy['y']
def loadfits(n):
hdulist = fits.open(chuefile+'%d.fits'%n)
return hdulist[0].data,hdulist[1].data
for f in [loadh5f,loadnpy,loadnpz,loadfits]:
ttt = []
ttt_std = []
nnn = 10**np.arange(3,9)
for n in nnn:
tt = []
for i in range(5):
t1 = time.time()
x,y = f(n)
tt.append(time.time()-t1)
ttt.append(tt)
ttt = np.log10(np.array(ttt))
std = np.array(ttt).std(1)
mean = np.array(ttt).mean(1)
nnn = np.log10(nnn)
plt.plot(nnn,mean)
plt.fill_between(nnn,mean-std,mean+std,alpha=0.3)
plt.xlabel('log(จำนวนข้อมูล)',family='Tahoma')
plt.ylabel('log(เวลา)',family='Tahoma')
plt.legend(['h5','npy','npz','fits'])
plt.show()
ติดตามอัปเดตของบล็อกได้ที่แฟนเพจ