太长了;的python 中可用的各种压缩算法 gzip
, bz2
, lzma
等,哪个有最好的减压表现?
完整讨论:
Python 3 有用于压缩/解压缩数据的各种模块包括gzip
, bz2
and lzma
. gzip
and bz2
此外,您还可以设置不同的压缩级别。
如果我的目标是平衡文件大小(/压缩比)和解压缩速度(压缩速度不是问题),那么哪个将是最佳选择?解压速度比文件大小更重要,但由于所讨论的未压缩文件每个约为 600-800MB(32 位 RGB .png 图像文件),而且我有十几个,我确实想要some压缩。
┌────────────┬────────────────────────┬───────────────┬─────────────┐
│ Python Ver │ Library/Method │ Read/unpack + │ Compression │
│ │ │ Decompress (s)│ Ratio │
├────────────┼────────────────────────┼───────────────┼─────────────┤
│ 3.7.2 │ pillow (PIL.Image) │ 4.0 │ ~0.006 │
│ 3.7.2 │ Qt (QImage) │ 3.8 │ ~0.006 │
│ 3.7.2 │ numpy (uncompressed) │ 0.8 │ 1.0 │
│ 3.7.2 │ gzip (compresslevel=9) │ ? │ ? │
│ 3.7.2 │ gzip (compresslevel=?) │ ? │ ? │
│ 3.7.2 │ bz2 (compresslevel=9) │ ? │ ? │
│ 3.7.2 │ bz2 (compresslevel=?) │ ? │ ? │
│ 3.7.2 │ lzma │ ? │ ? │
├────────────┼────────────────────────┼───────────────┼─────────────┤
│ 3.7.3 │ ? │ ? │ ? │
├────────────┼────────────────────────┼───────────────┼─────────────┤
│ 3.8beta1 │ ? │ ? │ ? │
├────────────┼────────────────────────┼───────────────┼─────────────┤
│ 3.8.0final │ ? │ ? │ ? │
├────────────┼────────────────────────┼───────────────┼─────────────┤
│ 3.5.7 │ ? │ ? │ ? │
├────────────┼────────────────────────┼───────────────┼─────────────┤
│ 3.6.10 │ ? │ ? │ ? │
└────────────┴────────────────────────┴───────────────┴─────────────┘
示例 .png 图像:举个例子,以这个 5.0Mb png 图像,阿拉斯加海岸线的相当高分辨率的图像.
png/PIL 案例的代码(加载到numpy
array):
from PIL import Image
import time
import numpy
start = time.time()
FILE = '/path/to/file/AlaskaCoast.png'
Image.MAX_IMAGE_PIXELS = None
img = Image.open(FILE)
arr = numpy.array(img)
print("Loaded in", time.time()-start)
在我使用 Python 3.7.2 的机器上,此负载大约需要 4.2 秒。
或者,我可以加载通过选择上面创建的数组生成的未压缩的 pickle 文件。
未压缩pickle负载情况的代码:
import pickle
import time
start = time.time()
with open('/tmp/test_file.pickle','rb') as picklefile:
arr = pickle.load(picklefile)
print("Loaded in", time.time()-start)
在我的机器上,从这个未压缩的 pickle 文件加载大约需要 0.8 秒。