4 回答

TA貢獻1856條經驗 獲得超5個贊
從絕對意義上來講效率不一樣。取決于執行邏輯、輸入值等。
比較 object.Equals(object, null) 和 string.IsNullOrEmpty, object.Equals(object, object) 性能較差。
通過Reflector看反匯編:
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static bool Equals(object objA, object objB)
{
??? return ((objA == objB) || (((objA != null) && (objB != null)) && objA.Equals(objB)));
}
如果用 obj == null 代替 object.Equals(object, null)則性能與string.IsNullOrEmpty相當。
假如用obj == null 直接比較,在 obj = string = "" 的情況下 (obj == null) 性能好于 string.IsNullOrEmpty(string). 原因不難理解,string.IsNullOrEmpty多了一個判斷。
同樣看一下string.IsNullOrEmpty反匯編的結果·
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static bool IsNullOrEmpty(string value)
{
??? if (value != null)
??? {
??????? return (value.Length == 0);
??? }
??? return true;
}
it depends:)
?
有了TryParse 就用它吧~~
- 4 回答
- 0 關注
- 473 瀏覽
添加回答
舉報