2015-04-05 108 views
0

我在C#應用程序中測試了備用的反序列化方法。我正在嘗試ServiceStack(4.0.38),我發現了一些奇怪的行爲。這裏是我的測試代碼:ServiceStack,首次反序列化的緩慢?

public class BasicObject 
    { 
     public List<string> Stuff { get; set; } 

     public BasicObject() 
     { 
      Stuff = new List<string>(); 
     } 
    } 

    private void CustomTesting() 
    { 
     var largeObject = new BasicObject(); 
     var mediumObject = new BasicObject(); 
     var smallObject = new BasicObject(); 
     //Populate this shiz 
     for (int i = 0; i < 100000; i++) 
     { 
      if (i < 50000) 
       mediumObject.Stuff.Add("HelloWorld"); 

      if (i < 2000) 
       smallObject.Stuff.Add("HelloWorld"); 

      largeObject.Stuff.Add("HelloWorld"); 
     } 

     //Serialize, save to disk 
     using(var stream = new MemoryStream()) 
     { 
      JsonSerializer.SerializeToStream<BasicObject>(largeObject, stream); 
      File.WriteAllBytes("C:\\Temp\\Large", stream.ToArray()); 
     } 
     using (var stream = new MemoryStream()) 
     { 
      JsonSerializer.SerializeToStream<BasicObject>(mediumObject, stream); 
      File.WriteAllBytes("C:\\Temp\\Medium", stream.ToArray()); 
     } 
     using (var stream = new MemoryStream()) 
     { 
      JsonSerializer.SerializeToStream<BasicObject>(smallObject, stream); 
      File.WriteAllBytes("C:\\Temp\\Small", stream.ToArray()); 
     } 

     var watch = new Stopwatch(); 

     using (var stream = new MemoryStream(File.ReadAllBytes("C:\\Temp\\Large"))) 
     { 
      watch.Start(); 
      var test = JsonSerializer.DeserializeFromStream<BasicObject>(stream); 
      watch.Stop(); 
     } 
     var timeTakenLarge = watch.Elapsed.Milliseconds; 
     watch.Restart(); 

     using (var stream = new MemoryStream(File.ReadAllBytes("C:\\Temp\\Medium"))) 
     { 
      watch.Start(); 
      var test = JsonSerializer.DeserializeFromStream<BasicObject>(stream); 
      watch.Stop(); 
     } 
     var timeTakenMedium = watch.Elapsed.Milliseconds; 
     watch.Restart(); 

     using (var stream = new MemoryStream(File.ReadAllBytes("C:\\Temp\\Small"))) 
     { 
      watch.Start(); 
      var test = JsonSerializer.DeserializeFromStream<BasicObject>(stream); 
      watch.Stop(); 
     } 
     var timeTakenSmall = watch.Elapsed.Milliseconds; 
     watch.Restart(); 

     Console.Clear(); 
     Console.WriteLine(string.Format("{0} {1} {2}", timeTakenLarge, timeTakenMedium, timeTakenSmall)); 
     Console.ReadKey(); 
    } 

很簡單,拿一個基本的對象,用3個不同範圍的數據填充它。將其序列化,保存到磁盤。然後,將保存的文件保存在內存流中,查看ServiceStack反序列化需要多長時間。這裏是我的輸出(以毫秒爲單位的時間)

 
Deserialization Order: Large Med Small 
Display Order: Large Med Small 
209 61 1 
222 73 1 
178 57 1 
174 60 1 

Deserialization Order: Small Med Large 
Display Order: Large Med Small 
145 48 66 
128 72 63 
100 58 79 
98 55 57 

Deserialization Order: Med Small Large 
Display Order: Large Med Small 
106 130 1 
155 114 1 
106 199 1 
115 217 1 

出於某種原因,而且,任何反序列化時首先會有點慢。爲什麼是這樣? ServiceStack可能緩存關於幕後對象的信息?

回答

1

大多數串行器針對運行時性能進行了優化,其中由於創建並編譯包含最佳代碼路徑的所有委託,第一次迭代將很慢,然後將其緩存並重新用於隨後對緩存類型的序列化。

相關問題