HDF5: Python 的h5py与Julia的HDF5库读取效率比较,不差上下

126 篇文章 8 订阅
79 篇文章 6 订阅

封装一下h5py的读写array库,全数值型

def h5py_write_arr(save_path,data):
    f = h5py.File(save_path,'w')
    try:
        f['data'] =data
    finally:
        f.close()
        
def h5py_read_arr(save_path):
    f = h5py.File(save_path,'r')
    try:
        data =f['data'][()]
    finally:
        f.close()
    return data

用python 把一个array持久化为h5.

path = "C:\Users\rustr\Desktop\my.h5"

比较一下读取的速度情况

1、用 h5py_read_arr:

    t1 =t.time()
    data = h5py_read_arr(path)
    
    print("read h5 cost time:",t.time()-t1)

time:",t.time()-t1)
read h5 cost time: 0.14444708824157715

t1 =t.time() data = h5py_read_arr(path)

print(“read h5 cost time:”,t.time()-t1) read h5 cost time:
0.2049427032470703

t1 =t.time() data = h5py_read_arr(path)

print(“read h5 cost time:”,t.time()-t1) read h5 cost time:
0.27478766441345215

t1 =t.time() data = h5py_read_arr(path)

print(“read h5 cost time:”,t.time()-t1) read h5 cost time:
0.18096709251403809

2、用Julia:

using HDF5

function read_h5(file)
    fid = HDF5.h5open(file,"r")
    try
        data = read(fid,"data")
    finally
        close(fid)
    end
    return data
end
file = s"C:\Users\rustr\Desktop\my.h5"
@time data =read_h5(file);
size(data)

或者直接来一行:

@time data = h5read(file,"data") # 简单,一行搞定

结果如下:

julia> @time data =read_h5(file);
  0.178845 seconds (2.56 k allocations: 397.228 MiB, 17.72% gc time)

julia> @time data =read_h5(file);
  0.181711 seconds (59 allocations: 397.107 MiB, 20.64% gc time)

3、结论

(1)python 的h5py库是可以被Julia的HDF5库读取的;有可能在h5层次成为一个文件的标准;其它库生成的文件,并不一定,比如pdstore函数。

(2)Julia和python差不多。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值