BinaryFormatter alternatives

前端 未结 3 2269
旧巷少年郎
旧巷少年郎 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:57

    This is more of a comment but it's way too much for one... I'm not able to reproduce your results. There is, however, some additional overhead with the struct.

    My testing:

    -------------------------------------------------------------------------------
    Testing array of structs
    
    Size of double:  8
    Size of doubles.bin:  16777244
    Size per array item:  8
    Milliseconds to serialize:  143
    -------------------------------------------------------------------------------
    -------------------------------------------------------------------------------
    Testing array of structs
    
    Size of dd struct:  16
    Size of structs.bin:  52428991
    Size per array item:  25
    Milliseconds to serialize:  9678
    -------------------------------------------------------------------------------
    

    Code:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.IO;
    using System.Diagnostics;
    
    namespace ConsoleApplication5
    {
        class Program
        {
            static void Main(string[] args)
            {
                TestDoubleArray();
                TestStructArray();
            }
    
            private static void TestStructArray()
            {
    
                Stopwatch stopWatch = new Stopwatch();
                stopWatch.Start();
    
                dd[] d1 = new dd[2097152];
                BinaryFormatter f1 = new BinaryFormatter();
                f1.Serialize(File.Create("structs.bin"), d1);
    
                stopWatch.Stop();
    
                Debug.WriteLine("-------------------------------------------------------------------------------");
                Debug.WriteLine("Testing array of structs");
                Debug.WriteLine("");
                Debug.WriteLine("Size of dd struct:  " + System.Runtime.InteropServices.Marshal.SizeOf(typeof(dd)).ToString());
                FileInfo fi = new FileInfo("structs.bin");
                Debug.WriteLine("Size of structs.bin:  " + fi.Length.ToString());
                Debug.WriteLine("Size per array item:  " + (fi.Length / 2097152).ToString());
                Debug.WriteLine("Milliseconds to serialize:  " + stopWatch.ElapsedMilliseconds);
                Debug.WriteLine("-------------------------------------------------------------------------------");
            }
    
            static void TestDoubleArray()
            {
                Stopwatch stopWatch = new Stopwatch();
                stopWatch.Start();
    
                double[] d = new double[2097152];
                BinaryFormatter f = new BinaryFormatter();
                f.Serialize(File.Create("doubles.bin"), d);
    
                stopWatch.Stop();
    
                Debug.WriteLine("-------------------------------------------------------------------------------");
                Debug.WriteLine("Testing array of structs");
                Debug.WriteLine("");
                Debug.WriteLine("Size of double:  " + sizeof(double).ToString());
                FileInfo fi = new FileInfo("test.bin");
                Debug.WriteLine("Size of doubles.bin:  " + fi.Length.ToString());
                Debug.WriteLine("Size per array item:  " + (fi.Length / 2097152).ToString());
                Debug.WriteLine("Milliseconds to serialize:  " + stopWatch.ElapsedMilliseconds);
                Debug.WriteLine("-------------------------------------------------------------------------------");
            }
    
            [Serializable]
            struct dd
            {
                double a;
                double b;
            }
        }
    }
    

提交回复
热议问题