我正在使用zlib压缩一些数据,我遇到了一个奇怪的问题:使用python压缩的数据小于使用C ++压缩的数据。我有130MB的模拟数据要保存压缩(所有必要数据的文件太多)。
使用C ++,我有类似的东西:
//calculate inputData (double * 256 * 256 * 256)
unsigned int length = inputLength;
unsigned int outLength = length + length/1000 + 12 + 1;
printf("Length: %d %d\n", length, outLength);
Byte *outData = new Byte[outLength];
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.next_in = (Byte *) inputData;
strm.avail_in = length;
deflateInit(&strm, -1);
do {
strm.next_out = outData;
strm.avail_out = outLength;
deflate(&strm, Z_FINISH);
unsigned int have = outLength - strm.avail_out;
fwrite(outData, 1, have, output);
} while(strm.avail_out == 0);
deflateEnd(&strm);
delete[] outData;
使用C ++的结果大约是120MB,这几乎不是我所期望的,因为原始版本接近130MB。
在python中:
from array import array
import zlib
// read data from uncompressed file
arrD = array.array('d', data)
file.write(zlib.compress(arrD))
使用相同输入数据使用Python的结果大约为50MB,不到一半。 C ++代码主要基于在python' s implementation中使用的代码,这使得这个问题更加怪异。
对于C ++,我使用的是自己编译的Zlib 1.2.8的Visual Studio 2010 Profession。 对于Python,我使用官方的python 3.4.2。