2013-12-19 56 views
1

我想爲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) 
     { 
     } 
+0

看起來它會做到這一點不序列化更容易,更快。 –

+0

您必須找到延遲的位置。 1)你的行爲? 2)在需要反序列化的大數據文件上......在第二種情況下,使用不同的庫,例如查看protobuf-net:https://code.google.com/p/protobuf-net/ – Aristos

回答