Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. // This can be a method on a generic class, it does not matter.
  2. public void DoSomething<T>(T instance)
  3. {
  4. if (instance == null) throw new ArgumentNullException("instance");
  5.  
  6. static void DoSomething<T>(T instance)
  7. {
  8. if (EqualityComparer<T>.Default.Equals(instance, null))
  9. throw new ArgumentNullException("instance");
  10.  
  11. if (((object) instance) == null)
  12. throw new ArgumentNullException("instance");
  13.  
  14. static bool IsNullCast<T>(T instance)
  15. {
  16. return ((object) instance == null);
  17. }
  18.  
  19. private const int Iterations = 100000000;
  20.  
  21. static void Test(Action a)
  22. {
  23. // Start the stopwatch.
  24. Stopwatch s = Stopwatch.StartNew();
  25.  
  26. // Loop
  27. for (int i = 0; i < Iterations; ++i)
  28. {
  29. // Perform the action.
  30. a();
  31. }
  32.  
  33. // Write the time.
  34. Console.WriteLine("Time: {0} ms", s.ElapsedMilliseconds);
  35.  
  36. // Collect garbage to not interfere with other tests.
  37. GC.Collect();
  38. }
  39.  
  40. Console.WriteLine("Value type");
  41. Test(() => IsNullCast(1));
  42. Console.WriteLine();
  43.  
  44. Console.WriteLine("Non-null nullable value type");
  45. Test(() => IsNullCast((int?)1));
  46. Console.WriteLine();
  47.  
  48. Console.WriteLine("Null nullable value type");
  49. Test(() => IsNullCast((int?)null));
  50. Console.WriteLine();
  51.  
  52. // The object.
  53. var o = new object();
  54.  
  55. Console.WriteLine("Not null reference type.");
  56. Test(() => IsNullCast(o));
  57. Console.WriteLine();
  58.  
  59. // Set to null.
  60. o = null;
  61.  
  62. Console.WriteLine("Not null reference type.");
  63. Test(() => IsNullCast<object>(null));
  64. Console.WriteLine();
  65.  
  66. Value type
  67. Time: 1171 ms
  68.  
  69. Non-null nullable value type
  70. Time: 18779 ms
  71.  
  72. Null nullable value type
  73. Time: 9757 ms
  74.  
  75. Not null reference type.
  76. Time: 812 ms
  77.  
  78. Null reference type.
  79. Time: 849 ms
  80.  
  81. static class IsNullHelper<T>
  82. {
  83. private static Predicate<T> CreatePredicate()
  84. {
  85. // If the default is not null, then
  86. // set to false.
  87. if (((object) default(T)) != null) return t => false;
  88.  
  89. // Create the expression that checks and return.
  90. ParameterExpression p = Expression.Parameter(typeof (T), "t");
  91.  
  92. // Compare to null.
  93. BinaryExpression equals = Expression.Equal(p,
  94. Expression.Constant(null, typeof(T)));
  95.  
  96. // Create the lambda and return.
  97. return Expression.Lambda<Predicate<T>>(equals, p).Compile();
  98. }
  99.  
  100. internal static readonly Predicate<T> IsNull = CreatePredicate();
  101. }
  102.  
  103. Console.WriteLine("Value type");
  104. Test(() => IsNullHelper<int>.IsNull(1));
  105. Console.WriteLine();
  106.  
  107. Console.WriteLine("Non-null nullable value type");
  108. Test(() => IsNullHelper<int?>.IsNull(1));
  109. Console.WriteLine();
  110.  
  111. Console.WriteLine("Null nullable value type");
  112. Test(() => IsNullHelper<int?>.IsNull(null));
  113. Console.WriteLine();
  114.  
  115. // The object.
  116. var o = new object();
  117.  
  118. Console.WriteLine("Not null reference type.");
  119. Test(() => IsNullHelper<object>.IsNull(o));
  120. Console.WriteLine();
  121.  
  122. Console.WriteLine("Null reference type.");
  123. Test(() => IsNullHelper<object>.IsNull(null));
  124. Console.WriteLine();
  125.  
  126. Value type
  127. Time: 959 ms
  128.  
  129. Non-null nullable value type
  130. Time: 1365 ms
  131.  
  132. Null nullable value type
  133. Time: 788 ms
  134.  
  135. Not null reference type.
  136. Time: 604 ms
  137.  
  138. Null reference type.
  139. Time: 646 ms
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement