为什么在Python中保存/加载数据比matlab需要更多的空间/时间? [英] Why does saving/loading data in python take a lot more space/time than matlab?

查看:168
本文介绍了为什么在Python中保存/加载数据比matlab需要更多的空间/时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些变量,包括字典,列表和numpy数组。我用下面的代码保存所有的代码,其中obj = [var1,var2,...,varn]。



我的问题是当我在matlab中保存相应的变量时,输出文件比磁盘空间少得多在Python中执行。同样,从磁盘加载变量需要更多的时间在python中加载内存比matlab。

  with open文件名,'wb')作为输出:
pickle.dump(obj,output,pickle.HIGHEST_PROTOCOL)

谢谢

解决方案

试试这个:

到磁盘

  import gzip 
gz = gzip.open(filename +'.gz','wb')
gz.write(pickle.dumps(obj,pickle.HIGHEST_PROTOCOL))
gz.close()

从磁盘加载

  import gzip 
gz = gzip.open(filename +'。 gz','rb')
obj = pickle.loads(gz.read())
gz.close()


I have some variables, which include dictionaries, list of list, and numpy arrays. I save all of them with the following code, where obj=[var1,var2,...,varn]. The variables size is small enough to be loaded in memory.

My problem is when I save the corresponding variables in matlab the output file takes a lot less space on the disk than doing it in python. Similarly, loading the variables from the disk takes a lot more time to be loaded in memory in python than matlab.

with open(filename, 'wb') as output:
    pickle.dump(obj, output, pickle.HIGHEST_PROTOCOL)

Thanks

解决方案

Try this:

To save to disk

import gzip
gz = gzip.open(filename + '.gz', 'wb')
gz.write(pickle.dumps(obj, pickle.HIGHEST_PROTOCOL))
gz.close()

To load from disk

import gzip
gz = gzip.open(filename + '.gz', 'rb')
obj = pickle.loads(gz.read())
gz.close()

这篇关于为什么在Python中保存/加载数据比matlab需要更多的空间/时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆