public sealed class String : IComparable, ICloneable, IConvertible, IEnumerable, IComparable<string> ...{ static String() ...{ string.Empty = ""; // Code here } // Code here public static readonly string Empty; public static bool operator ==(string a, string b) ...{ return string.Equals(a, b); } public static bool Equals(string a, string b) ...{ if (a == b) ...{ return true; } if ((a != null) && (b != null)) ...{ return string.EqualsHelper(a, b); } return false; } private static unsafe bool EqualsHelper(string ao, string bo) ...{ // Code here int num1 = ao.Length; if (num1 != bo.Length) ...{ return false; } // Code here } private extern int InternalLength(); public int Length ...{ get ...{ return this.InternalLength(); } } // Code here }
Rotor里面String類的代碼與此沒什么不同,只是沒有EqualsHelper方法,代之以如下的聲明: public extern bool Equals(String value); 進一步分析: 首先是Empty法,由于String.Empty是一個靜態只讀域,只會被創建一次(在靜態構造函數中)。但當我們使用Empty法進行判空時,.NET還會依次展開調用以下的方法,而后兩個方法內部還會進行對象引用判等! public static bool operator ==(string a, string b); public static bool Equals(string a, string b); private static unsafe bool EqualsHelper(string ao, string bo); 若使用General法判等的話,情況就“更勝一籌”了!因為.NET除了要依次展開調用上面三個方法之外,還得首先創建一個臨時的空字符串實例,如果你要進行大量的比較,這恐怕是想一想就很嚇人了! 而對于Length法,我們就可以繞過上面這些繁瑣的步驟,直接進行整數(字符串長度)判等,我們知道,大多數情況下,整數判等都要來得快(我實在想不出比它更快的了,在32位系統上,System.Int32運算最快了)! 另外,我們還可以看到,在EqualsHelper方法里面.NET會先使用Length法來進行判等!可惜的是我無法獲得InternalLength方法的代碼。但我在Mono的源代碼里面看到更簡明的實現: