【发布时间】:2013-12-19 07:09:48
【问题描述】:
我正在尝试在 C# 中对字典进行序列化和反序列化。问题是,当我进行反序列化时,需要 3-4 分钟。文本文件大小为 4.3 MB 这是我的代码.. 请帮帮我。
[Serializable]
public class WordTag
{
public String Word, Tag;
public double prob;
public WordTag()
{ }
public WordTag(String W, String T)
{
Word = W;
Tag = T;
}
[Serializable]
public class EqualityComparer : IEqualityComparer<WordTag>
{
public bool Equals(WordTag x, WordTag y)
{
return x.Word == y.Word && x.Tag == y.Tag;
}
public int GetHashCode(WordTag x)
{
return base.GetHashCode();
}
}
}
.....................
try
{
using (Stream stream = File.Open("e:\\WordTagFreq.txt", FileMode.Open))
{
BinaryFormatter bin = new BinaryFormatter();
WordTagDic.Clear();
WordTagDic = (Dictionary<WordTag, int>)bin.Deserialize(stream);
}
}
catch (IOException)
{
}
【问题讨论】:
-
看起来没有序列化会更容易和更快。
-
你必须找到延迟在哪里。 1)关于你的行为? 2)在需要反序列化的大数据文件上......在第二种情况下,使用不同的库,例如查看protobuf-net:code.google.com/p/protobuf-net
标签: c# asp.net serialization deserialization