How to increase deserialization speed?

时间秒杀一切 提交于 2019-12-23 09:38:05

问题


Serializing/deserializing with BinaryFormatter, resulting serialized file is ~80MB in size. The deserialization takes a few minutes. How could I improve on this? Here's the deserialization code:

    public static Universe DeserializeFromFile(string filepath)
    {
        Universe universe = null;

        FileStream fs = new FileStream(filepath, FileMode.Open);

        BinaryFormatter bf = new BinaryFormatter();
        try
        {
            universe = (Universe)bf.Deserialize(fs);
        }
        catch (SerializationException e)
        {
            Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
            throw;
        }
        finally
        {
            fs.Close();
        }

        return universe;
    }

Maybe read all to memory prior to deserializing or use some other serialization technique?


回答1:


Try UnsafeDeserialize. It is said to improve speed.




回答2:


Please take a look at this thread.




回答3:


Try reading the file into a memory stream first in one go, then deserialize using the memory stream.




回答4:


How complex is the data? If it is an object tree (rather than a full graph), then you might get some interesting results from trying protobuf-net. It is generally pretty easy to fit onto existing classes, and is generally much smaller, faster, and less brittle (you can change the object model without trashing the data).

Disclosure: I'm the author, so might be biased - but it really isn't terrible... I'd happily lend some* time to help you try it, though.

*=within reason




回答5:


Implement ISerializable in the Universe class



来源:https://stackoverflow.com/questions/1620663/how-to-increase-deserialization-speed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!