Showing posts with label clr. Show all posts
Showing posts with label clr. Show all posts

Thursday, April 30, 2009

Reflection performance

int x = 10; Assembly a = Assembly.Load("mscorlib"); Type t1 = typeof(int); Type t2 = x.GetType(); Type t3 = a.GetType("System.Int32"); Type t4 = Type.GetType("System.Int32"); Console.WriteLine(t1 == t2); Console.WriteLine(t2 == t3); Console.WriteLine(t3 == t4);

All of the equality comparisons will evaluate to true and, assuming that Type's Equals method is transitive (as all Equals implementations are supposed to be), then we can infer that t1 == t2 == t3 == t4. Loading t1 is the most efficient, t2 is a close second, and t3 and t4 are horribly inefficient. t3 is slightly more efficient because it only searches mscorlib.dll, whereas t4 searches all loaded assemblies. In fact, in a little test program I wrote, t2 is twice as slow as t1, t3 is 100 times slower than t2, and t4 is twice as slow as t3.

Friday, March 27, 2009

Nullable and ?? Operator

After reading the article, The C# ?? null coalescing operator (and using it with LINQ), I did a quick test like the following code. It seems the compiler does different thing for Nullabe type, and other reference type with ?? operator. In fact, ?? should not be called operator, it is shortcut for compiler instruction. It will be more effective to use "t1.GetValueOfDefault()" than "t1 ?? 0" . But latter seems more language built-in feature than API.

int? t1 = null; int t2 = t1 ?? 0; int t3 = (t1.HasValue ? t1.GetValueOrDefault() : 0); int t4 = t1.GetValueOrDefault(); DateTime? d1 = null; DateTime d2 = d1.GetValueOrDefault(); string s = null; string s2 = s ?? "fred"; //reflector translate to the following int? t1 = null; int? d3 = t1; int t2 = d3.HasValue ? d3.GetValueOrDefault() : 0; int t3 = t1.HasValue ? t1.GetValueOrDefault() : 0; int t4 = t1.GetValueOrDefault(); DateTime d2 = null.GetValueOrDefault(); string s = null; string s2 = s ?? "fred";

Thursday, March 26, 2009

Generic method

The following code shows some example of generic method

//the T can be inferred from the type of input parameter public static T DoByInferType<T>(T input) { return default(T); } //the Toutput cannot be inferred by the input parameter public static Toutput DoByExplicitType<Tinput, Toutput>(Tinput input) { return default(Toutput); } int result = Utililty.DoByInferType(1); //or (the <int> is optional) //int result = Utililty.DoByInferType<int>(1); //<string, int> has be explicit int result2 = Utililty.DoByExplicitType<string, int>("1"); List<string> l = new List<string>(); Converter<string, int> converterUsingDelegate = delegate(string theString) { return int.Parse(theString); }; //public List<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter); //the TOutput can be inferred from the input paramether List<int> intList1 = l.ConvertAll(converterUsingDelegate);

Saturday, January 17, 2009

What is the cost of catching an exception

We all known that following an exception without using it is quite silly. How does it really hurt performance. It turns out that difference is very small.

DateTime entry = DateTime.Now; for (int i = 0; i < 100000; i++) { try { throw new Exception("something wrong"); } //catch catch (Exception ex) { } } DateTime exit = DateTime.Now; TimeSpan ts = exit - entry; Console.WriteLine(ts.TotalMilliseconds); //catch 2593.7002 //no catch 2531.2014