本文介绍了如何提高反序列化的速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
串行化/ BinaryFormatter的与反序列化,导致系列化文件〜大小80MB。反序列化需要几分钟的时间。我怎么能提高呢?这里的反序列化的代码:
公共静态宇宙DeserializeFromFile(字符串文件路径)
{
宇宙宇宙= NULL ;
的FileStream FS =新的FileStream(文件路径,FileMode.Open);
BinaryFormatter的BF =新的BinaryFormatter();
试
{
=宇宙(宇宙)bf.Deserialize(FS);
}
赶上(SerializationException E)
{
Console.WriteLine(无法反序列化原因:+ e.Message);
抛出;
}
终于
{
fs.Close();
}
返回宇宙;
}
也许阅读所有内存到反序列化或使用一些其他的序列化技术之前
解决方案
尝试 UnsafeDeserialize
。据说这是提高速度。
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?
解决方案
Try UnsafeDeserialize
. It is said to improve speed.
这篇关于如何提高反序列化的速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!