fork download
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using System.Diagnostics;
  5. using System.Globalization;
  6. using System.Collections.Generic;
  7. using System.Runtime.Serialization.Formatters.Binary;
  8.  
  9. public class Test
  10. {
  11. [Serializable]
  12. public class DataItem
  13. {
  14. public Int32 Property1 { get; set; }
  15. public String Property2 { get; set; }
  16. public Int32 Property3 { get; set; }
  17.  
  18. public DataItem Parent;
  19. public List<DataItem> Children;
  20. }
  21.  
  22. public static void Main()
  23. {
  24. DataItem root = CreateTestData();
  25. DataItem testItems1 = Clone(root);
  26. //DataItem testItems2 = CopyProperties<DataItem, DataItem>(root);
  27.  
  28.  
  29. Measure("Serialize",
  30. delegate
  31. {
  32. DataItem items2 = Clone(root);
  33. });
  34. //Measure("Reflection",
  35. // delegate
  36. // {
  37. // DataItem items3 = CopyProperties<DataItem, DataItem>(root);
  38. // });
  39. Console.ReadLine();
  40. }
  41.  
  42. private static int _levels = 5;
  43. private static int _childCount = 5;
  44. private static int _identity = 0;
  45.  
  46. private static int GetNextId()
  47. {
  48. return _identity++;
  49. }
  50.  
  51. static DataItem CreateTestData()
  52. {
  53. DataItem root = new DataItem { Property1 = GetNextId(), Property2 = GetNextId().ToString(), Property3 = GetNextId(), Children = new List<DataItem>() };
  54. CreateChildren(root);
  55. return root;
  56. }
  57.  
  58. static void CreateChildren(DataItem parent, int level = 0)
  59. {
  60. for (int i = 0; i < _childCount; i++)
  61. {
  62. DataItem child = new DataItem
  63. {
  64. Property1 = GetNextId(),
  65. Property2 = GetNextId().ToString(),
  66. Property3 = GetNextId(),
  67. Parent = parent
  68. };
  69. parent.Children.Add(child);
  70.  
  71. if (level < _levels)
  72. {
  73. child.Children = new List<DataItem>();
  74. CreateChildren(child, level + 1);
  75. }
  76. }
  77. }
  78.  
  79. static T Clone<T>(T target)
  80. {
  81. using (MemoryStream stream = new MemoryStream())
  82. {
  83. BinaryFormatter formatter = new BinaryFormatter();
  84. formatter.Serialize(stream, target);
  85. stream.Position = 0;
  86. T result = (T)formatter.Deserialize(stream);
  87. return result;
  88. }
  89. }
  90. //static void CopyProperties<S, T>(S source, T target) where T : class
  91. //{
  92. // Type type = typeof(T);
  93. // PropertyInfo[] properties = source.GetType().GetProperties();
  94. // foreach (PropertyInfo property in properties)
  95. // {
  96. // PropertyInfo targetProperty = type.GetProperty(property.Name);
  97. // if (targetProperty != null)
  98. // targetProperty.SetValue(target, property.GetValue(source, null), null);
  99. // }
  100. //}
  101. //static T[] CopyProperties<S, T>(S[] source) where T : class, new()
  102. //{
  103. // if (source == null)
  104. // throw new ArgumentNullException("source");
  105.  
  106. // T[] result = new T[source.Length];
  107. // for (Int32 loop = 0; loop < source.Length; loop++)
  108. // {
  109. // result[loop] = new T();
  110. // CopyProperties(source[loop], result[loop]);
  111. // }
  112. // return result;
  113. //}
  114. static void Measure(String message, Action method)
  115. {
  116. Int64 mem = GC.GetTotalMemory(true);
  117. Int64 gcCount = GC.CollectionCount(0);
  118.  
  119. DateTime now = DateTime.Now;
  120. Stopwatch stopwatch = Stopwatch.StartNew();
  121. method();
  122. TimeSpan elapsed = stopwatch.Elapsed;
  123.  
  124. // DONTTOUCH:
  125. // It is intended to call GetTotalMemory with forceFullCollection: false
  126. // before CollectionCount call.
  127. Double tempMemDelta = Math.Max(GC.GetTotalMemory(false) - mem, 0);
  128. Int64 gcCountDelta = GC.CollectionCount(0) - gcCount;
  129.  
  130. String elapsedString = elapsed.TotalSeconds.ToString("00.0000", CultureInfo.InvariantCulture);
  131. String memDeltaInKb = (tempMemDelta / 1024).ToString("0.00 KB", CultureInfo.InvariantCulture);
  132.  
  133. Console.WriteLine(@"{0}:
  134. Elapsed: {1} s, MemDelta: {2,10}, GC count: {3}
  135. ", message, elapsedString, memDeltaInKb, gcCountDelta);
  136.  
  137. }
  138. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty