BinaryFormatter alternatives

前端 未结 3 2168
旧巷少年郎
旧巷少年郎 2020-12-30 04:37

A BinaryFormatter-serialized array of 128³ doubles, takes up 50 MB of space. Serializing an array of 128³ structs with two double fields takes up 150 MB an

3条回答
  •  囚心锁ツ
    2020-12-30 04:43

    Serializing means that metadata is added so that the data can be safely deserialized, that's what's causing the overhead. If you serialize the data yourself without any metadata, you end up with 16 MB of data:

    foreach (double d in array) {
       byte[] bin = BitConverter.GetBytes(d);
       stream.Write(bin, 0, bin.Length);
    }
    

    This of course means that you have to deserialize the data yourself also:

    using (BinaryReader reader = new BinaryReader(stream)) {
       for (int i = 0; i < array.Length; i++) {
          byte[] data = reader.ReadBytes(8);
          array[i] = BitConverter.ToDouble(data, 0);
       }
    }
    

提交回复
热议问题