使用gzip_compressor生成不同的文件大小
Using gzip_compressor yield different file sizes
我使用gzip_compressor()来压缩输出文件。为此我使用了两种方法。共有部分为
std::ofstream traceOut;
traceOut.open("log.gz", std::ios_base::out);
struct traceRec {
traceRec(uint64_t c) : cycle(c) {};
uint64_t cycle;
};
void writeTrace(traceRec &rec)
{
boost::iostreams::filtering_ostream o;
o.push(boost::iostreams::gzip_compressor());
o.push(traceOut);
// METHOD 1 OR 2
}
方法1
我使用 o.write(reinterpret_cast<const char*>(&rec.cycle), sizeof(rec.cycle));
使用此实现,文件大小为380K!!
方法2
我使用 traceOut << rec.cycle << std::endl;
使用此实现,文件大小为78K!!
那为什么它们的大小不同呢??另外,如果我不使用gzip_compressor直接写入文件
std::ofstream traceOut;
traceOut.open("log.gz", std::ios_base::out);
...
traceOut << rec.cycle << std::endl;
文件大小为78K。
所以有两个问题:
1-使用或不使用gzip_compressor
对文件大小没有影响
2-使用gzip_compressor
的不同实现产生不同的文件大小
你知道吗?
operator <<可能使用数字的文本表示形式,而write方法采用完整的可变大小。
例如,如果你有一个"13"的循环,在"写"的情况下,你将消耗8个字节,而在文本表示中你只消耗2个字节。
当压缩时,效果更加显著,因为当将数字作为文本时,只使用10个字符,(非常非常低的熵),所以它是高度冗余和可压缩的。
另一方面,如果循环计数器通常非常大(> 99999999),那么write方法将提供更好的压缩。
相关文章:
- 读取某些文件时出现分段错误,似乎与文件大小无关
- 构建挂起,即使是适度的文件大小
- 如何在Windows内核中获取文件大小
- 为什么 QFileSystemWatcher 会发出多个信号?而 QFileInfo 首次写入零文件大小
- 使用 C++ iOS::ate 获取错误的文件大小
- 相同的源代码,不同的可执行文件大小?
- 为什么未关闭的文件大小为 4 字节
- 读取二进制文件大小的短自上周以来停止工作
- 如何减小C++标准库libstdc++.so文件大小
- 文件大小和缓冲区过冲
- 如何从范围 v3 的istream_range中检索文件大小?
- 如何使用直接IO编写一个带有扩展文件大小的小文件
- 如何使用文件系统库获取"true"文件大小?(C++17)
- 如何限制文件大小,以便我的程序在变大之后创建一个新文件?并编辑新创建的文件的名称
- 如何使用C 17获取文件大小
- HttpQueryInfo 获取文件大小
- 二进制文件大小大于预期的 c++
- 如何计算预期的核心文件大小
- C :不正确的文件大小计算Winapi
- 减小 Wasm 文件大小(libc、optimization、emscripten)