我正在尝试将大的UInt16数组保存到一个文件中。positionCnt约为50000,stationCnt约为2500。直接保存,没有GZipStream,文件大小约为250MB,可通过外部压缩程序压缩到19MB。使用以下代码,文件大小为507MB。我做错了什么?
GZipStream cmp = new GZipStream(File.Open(cacheFileName, FileMode.Create), CompressionMode.Compress);
BinaryWriter fs = new BinaryWriter(cmp);
fs.Write((Int32)(positionCnt * stationCnt));
for (int p = 0; p < positionCnt; p++)
{
for (int s = 0; s < stationCnt; s++)
{
fs.Write(BoundData[p, s]);
}
}
fs.Close();发布于 2011-09-29 05:23:37
不管是什么原因,在快速阅读.Net中的GZip实现时,性能对一次写入的数据量很敏感,这对我来说并不明显。我将您的代码与几种写入GZipStream的样式进行了基准测试,发现最有效的版本是将大跨度写入磁盘。
在本例中,权衡的是内存,因为您需要根据所需的步长将short[,]转换为byte[]:
using (var writer = new GZipStream(File.Create("compressed.gz"),
CompressionMode.Compress))
{
var bytes = new byte[data.GetLength(1) * 2];
for (int ii = 0; ii < data.GetLength(0); ++ii)
{
Buffer.BlockCopy(data, bytes.Length * ii, bytes, 0, bytes.Length);
writer.Write(bytes, 0, bytes.Length);
}
// Random data written to every other 4 shorts
// 250,000,000 uncompressed.dat
// 165,516,035 compressed.gz (1 row strides)
// 411,033,852 compressed2.gz (your version)
}https://stackoverflow.com/questions/7588785
复制相似问题