Advertisement
Guest User

Object Cloning: Protobuf-net vs. Nuclex.Support

a guest
Aug 30th, 2014
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.98 KB | None | 0 0
  1. // Results on Win8.0/AMD64:
  2. //   ReflectionCloner took 340 ms
  3. //   ExpressionTreeCloner took 56 ms
  4. //   SerializationCloner took 3395 ms (3.4 s)
  5. //   Protobuf-net took 2856569 ms (48 m)
  6.  
  7. using System;
  8. using System.Diagnostics;
  9. using System.IO;
  10.  
  11. using ProtoBuf;
  12. using ProtoBuf.Meta;
  13.  
  14. using Nuclex.Support.Cloning;
  15.  
  16. namespace ConsoleApplication1 {
  17.  
  18.   class Program {
  19.  
  20.     public class FunkyType {
  21.  
  22.       public FunkyType Nest;
  23.  
  24.       public int SomeInt;
  25.  
  26.       public string SomeString;
  27.  
  28.       public float SomeFloat;
  29.  
  30.     }
  31.  
  32.     static void Main(string[] args) {
  33.       var x = new FunkyType() {
  34.         Nest = new FunkyType() {
  35.           SomeString = "More text"
  36.         },
  37.         SomeInt = 123,
  38.         SomeString = "Lotsa text",
  39.         SomeFloat = 456.789f
  40.       };
  41.  
  42.       MetaType stringSegmentMetaType = RuntimeTypeModel.Default.Add(
  43.         typeof(FunkyType), applyDefaultBehaviour: false
  44.       );
  45.       stringSegmentMetaType
  46.         .Add(1, "Nest")
  47.         .Add(2, "SomeInt")
  48.         .Add(3, "SomeString")
  49.         .Add(4, "SomeFloat");
  50.  
  51.  
  52.       FunkyType x2;
  53.  
  54.       // Reflection cloner
  55.       {
  56.         var w = Stopwatch.StartNew();
  57.  
  58.         for(int index = 0; index < 100000; ++index) {
  59.           x2 = ReflectionCloner.DeepFieldClone(x);
  60.         }
  61.  
  62.         Console.WriteLine("ReflectionCloner took " + w.ElapsedMilliseconds.ToString() + " ms");
  63.       }
  64.  
  65.       // Expression tree cloner
  66.       {
  67.         var w = Stopwatch.StartNew();
  68.  
  69.         for(int index = 0; index < 100000; ++index) {
  70.           x2 = ExpressionTreeCloner.DeepFieldClone(x);
  71.         }
  72.  
  73.         Console.WriteLine("ExpressionTreeCloner took " + w.ElapsedMilliseconds.ToString() + " ms");
  74.       }
  75.  
  76.       // Serialization cloner
  77.       {
  78.         var w = Stopwatch.StartNew();
  79.  
  80.         for(int index = 0; index < 100000; ++index) {
  81.           x2 = SerializationCloner.DeepFieldClone(x);
  82.         }
  83.  
  84.         Console.WriteLine("SerializationCloner took " + w.ElapsedMilliseconds.ToString() + " ms");
  85.       }
  86.  
  87.       // Protobuf cloner
  88.       using(var memoryStream = new MemoryStream()) {
  89.         var uselessContext = new SerializationContext();
  90.  
  91.         var w = Stopwatch.StartNew();
  92.  
  93.         for(int index = 0; index < 100000; ++index) {
  94.           using(
  95.             var writer = new ProtoWriter(
  96.               memoryStream, RuntimeTypeModel.Default, uselessContext
  97.             )
  98.           ) {
  99.             RuntimeTypeModel.Default.Serialize(writer, x);
  100.           }
  101.  
  102.           memoryStream.Position = 0;
  103.  
  104.           using(
  105.             var reader = new ProtoReader(
  106.               memoryStream, RuntimeTypeModel.Default, uselessContext
  107.             )
  108.           ) {
  109.             x2 = (FunkyType)RuntimeTypeModel.Default.Deserialize(
  110.               reader, null, typeof(FunkyType)
  111.             );
  112.           }
  113.         }
  114.  
  115.         Console.WriteLine("Protobuf-net took " + w.ElapsedMilliseconds.ToString() + " ms");
  116.  
  117.         memoryStream.Position = 0;
  118.       }
  119.  
  120.     }
  121.   }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement