1

pickle 和 savez_compressed 压缩体积对比

 2 years ago
source link: https://segmentfault.com/a/1190000041397848
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

pickle 和 savez_compressed 压缩体积对比

有的时候,我们需要把一些 numpy.ndarray 对象在网络中传输,这个时候就要寻找一种高效的、适合网络传输的序列化方式:

from mark import BASE_DIR
from numpy import ndarray
import numpy
import pickle

img1 = numpy.array([i for i in range(5120)])


# 方式一: 使用 pickle 序列化 numpy.ndarray
with open(BASE_DIR/'testing'/'001.bin', 'wb') as f:
    img1: ndarray
    f.write(pickle.dumps(img1))

# --- 结果 41111 bytes

# 方法二: 使用 pickle 序列化 python 的 list 对象
with open(BASE_DIR/'testing'/'002.bin', 'wb') as f:
    ins: list[int] = img1.tolist()
    f.write(pickle.dumps(ins))

# --- 结果 15130 bytes

# 方法三: 使用 numpy 的 savez 序列化 numpy.ndarray
numpy.savez(BASE_DIR/'testing'/'numpy_savez_test', img1)

# --- 结果 41224 bytes

# 方法四: 使用 numpy 的 savez_compressed (带压缩功能) 序列化 numpy.ndarray
numpy.savez_compressed(BASE_DIR/'testing'/'numpy_savez_compressed_test', img1)

numpy.load(BASE_DIR/'testing'/'numpy_savez_compressed_test')

# --- 结果 7982 bytes


""" 

"""
结论:
方法四 < 方法二 < 方法一 < 方法三
(7982)< (15130)< (41111)< (41224)

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK