Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This can be a method on a generic class, it does not matter.
- public void DoSomething<T>(T instance)
- {
- if (instance == null) throw new ArgumentNullException("instance");
- static void DoSomething<T>(T instance)
- {
- if (EqualityComparer<T>.Default.Equals(instance, null))
- throw new ArgumentNullException("instance");
- if (((object) instance) == null)
- throw new ArgumentNullException("instance");
- static bool IsNullCast<T>(T instance)
- {
- return ((object) instance == null);
- }
- private const int Iterations = 100000000;
- static void Test(Action a)
- {
- // Start the stopwatch.
- Stopwatch s = Stopwatch.StartNew();
- // Loop
- for (int i = 0; i < Iterations; ++i)
- {
- // Perform the action.
- a();
- }
- // Write the time.
- Console.WriteLine("Time: {0} ms", s.ElapsedMilliseconds);
- // Collect garbage to not interfere with other tests.
- GC.Collect();
- }
- Console.WriteLine("Value type");
- Test(() => IsNullCast(1));
- Console.WriteLine();
- Console.WriteLine("Non-null nullable value type");
- Test(() => IsNullCast((int?)1));
- Console.WriteLine();
- Console.WriteLine("Null nullable value type");
- Test(() => IsNullCast((int?)null));
- Console.WriteLine();
- // The object.
- var o = new object();
- Console.WriteLine("Not null reference type.");
- Test(() => IsNullCast(o));
- Console.WriteLine();
- // Set to null.
- o = null;
- Console.WriteLine("Not null reference type.");
- Test(() => IsNullCast<object>(null));
- Console.WriteLine();
- Value type
- Time: 1171 ms
- Non-null nullable value type
- Time: 18779 ms
- Null nullable value type
- Time: 9757 ms
- Not null reference type.
- Time: 812 ms
- Null reference type.
- Time: 849 ms
- static class IsNullHelper<T>
- {
- private static Predicate<T> CreatePredicate()
- {
- // If the default is not null, then
- // set to false.
- if (((object) default(T)) != null) return t => false;
- // Create the expression that checks and return.
- ParameterExpression p = Expression.Parameter(typeof (T), "t");
- // Compare to null.
- BinaryExpression equals = Expression.Equal(p,
- Expression.Constant(null, typeof(T)));
- // Create the lambda and return.
- return Expression.Lambda<Predicate<T>>(equals, p).Compile();
- }
- internal static readonly Predicate<T> IsNull = CreatePredicate();
- }
- Console.WriteLine("Value type");
- Test(() => IsNullHelper<int>.IsNull(1));
- Console.WriteLine();
- Console.WriteLine("Non-null nullable value type");
- Test(() => IsNullHelper<int?>.IsNull(1));
- Console.WriteLine();
- Console.WriteLine("Null nullable value type");
- Test(() => IsNullHelper<int?>.IsNull(null));
- Console.WriteLine();
- // The object.
- var o = new object();
- Console.WriteLine("Not null reference type.");
- Test(() => IsNullHelper<object>.IsNull(o));
- Console.WriteLine();
- Console.WriteLine("Null reference type.");
- Test(() => IsNullHelper<object>.IsNull(null));
- Console.WriteLine();
- Value type
- Time: 959 ms
- Non-null nullable value type
- Time: 1365 ms
- Null nullable value type
- Time: 788 ms
- Not null reference type.
- Time: 604 ms
- Null reference type.
- Time: 646 ms
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement