开发者

How can I code a set of numbers to a file so that the file has a minimal size?

开发者 https://www.devze.com 2023-01-14 18:37 出处:网络
I have to code some algorithm\'s result in a file. A Result is a list of blocks, each one defined by 5 numbers: 3 ints and 2 floats. The simplest way to code it in a file is of course plain text, for

I have to code some algorithm's result in a file. A Result is a list of blocks, each one defined by 5 numbers: 3 ints and 2 floats. The simplest way to code it in a file is of course plain text, for example like this:

12 56 81 0.34 1.67 
124 11 76 0.75 6.11 
...

It is also the worst way taking the file size into account.

How could I reduce the file size? If th开发者_运维问答ese were all integers, I would just convert them to bytes and simply write them as bytes to file and it would reduce the size a bit. But the floats are more problematic. Any ideas? In C# .NET


It is also the worst way taking the file size into account.

Is it? "12" and "56" would take 2 bytes each in ASCII or UTF-8 encoding. "124" would take 3 bytes, and there would roughly be 1 byte separator per item. Writing a binary int takes 4 bytes. A similar comparison holds for floats/doubles.

So if the sample is representative of your real data, Text is an easy and compact format. The flexibility is priceless.


You can always compress the file using a zip library like SharpZipLib.


Binary variable length would be the most compact (i.e. only store 11 bits for numbers that will only need 11 bits), but this rapidly becomes complex because you need to encode both type and length.

How about storing in text, but compressing (e.g. GZipStream), this will remove almost all (compression isn't perfect) without complex encoding.

Something like:

using (var fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write))
using (var gz = new GZipStream(fs, CompressionMode.Compress))
using (var writer = new StreamWriter(gz)) {
  foreach (var num in numbers) {
    writer.Write(num);
    writer.Write(' ');
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消