C# 斷定字符為空的6種辦法的效力實測比較。本站提示廣大學習愛好者:(C# 斷定字符為空的6種辦法的效力實測比較)文章只能為提供參考,不一定能成為您想要的結果。以下是C# 斷定字符為空的6種辦法的效力實測比較正文
C#中供給了相當豐碩的辦法或屬性來斷定一個字符能否為空,經常使用的辦法有以下6種
1. strTest== ""
2. strTest.Equals("")
3. strTest== string.Empty
4. strTest.Equals(string.Empty)
5. strTest.Length == 0
6. string.IsNullOrEmpty(strTest)
為了對以上6種辦法的效力,有個直不雅的感觸感染,我特地編寫了以下的測試代碼:
using System;
namespace StrTest
{
class Program
{
//界說3個字符串 以便測試在多種情形下 上面6種斷定辦法的速度
public static string strTest01 = "";
public static string strTest02 = string.Empty;
public static string strTest03 = "0123456789";
public static DateTime start, end; //界說開端時光 和 停止時光
public static TimeSpan ts; //界說兩個時光的距離
//**********************對strTest應用6種測試辦法*****************************
public static void Test(string strTest)
{
//string == ""
start = DateTime.Now;
for (int counter = 0; counter <= 100000000; counter++)
{
if (strTest == "")
{
}
}
end = DateTime.Now;
ts = end - start;
Console.WriteLine("string == /"/" 時光消費為 " + ts.TotalSeconds + " 秒");
//string.Equals("")
start = DateTime.Now;
for (int counter = 0; counter <= 100000000; counter++)
{
if (strTest.Equals(""))
{
}
}
end = DateTime.Now;
ts = end - start;
Console.WriteLine("string.Equals(/"/") 時光消費為 " + ts.TotalSeconds + " 秒");
//string == stirng.Empty
start = DateTime.Now;
for (int counter = 0; counter <= 100000000; counter++)
{
if (strTest == string.Empty)
{
}
}
end = DateTime.Now;
ts = end - start;
Console.WriteLine("string == string.Empty 時光消費為 " + ts.TotalSeconds + " 秒");
//string.Equals(string.Empty)
start = DateTime.Now;
for (int counter = 0; counter <= 100000000; counter++)
{
if (strTest.Equals(string.Empty))
{
}
}
end = DateTime.Now;
ts = end - start;
Console.WriteLine("string.Equals(string.Empty) 時光消費為 " + ts.TotalSeconds + " 秒");
//string.Length == 0
start = DateTime.Now;
for (int counter = 0; counter <= 100000000; counter++)
{
if (strTest.Length == 0)
{
}
}
end = DateTime.Now;
ts = end - start;
Console.WriteLine("string.Length == 0 時光消費為 " + ts.TotalSeconds + " 秒");
//string.IsNullOrEmpty(string)
start = DateTime.Now;
for (int counter = 0; counter <= 100000000; counter++)
{
if (string.IsNullOrEmpty(strTest))
{
}
}
end = DateTime.Now;
ts = end - start;
Console.WriteLine("string.IsNullOrEmpty(string) 時光消費為 " + ts.TotalSeconds + " 秒" + "/n");
}
static void Main(string[] args)
{
Console.WriteLine("=======================================");
Console.WriteLine("strTest = /"/" 的5種測試成果");
Console.WriteLine("=======================================");
Test(strTest01);
Console.WriteLine("=======================================");
Console.WriteLine("strTest = string.Emtpy 的5種測試成果");
Console.WriteLine("=======================================");
Test(strTest02);
Console.WriteLine("=======================================");
Console.WriteLine("strTest = /"0123456789/" 的5種測試成果");
Console.WriteLine("=======================================");
Test(strTest03);
Console.ReadLine(); //期待鍵盤的輸出 感化:使屏幕暫停在此處
}
}
}
我把能關的軟件都封閉失落了 盡量的屏障失落體系影響 而且讓6種辦法都運轉了1億次
第一次的截圖:

第二次的截圖:

從以上可以看出:字符串在三種情形下,string.Length == 0的效力無疑是最高的。
總結
1. strTest== "" 不推舉應用,只能斷定“值為空字符串”的字符串變量,並且效力比擬低。
2. strTest.Equals("") 不推舉應用,同 1。
3. strTest== string.Empty 不推舉應用,只能斷定“值為null”的字符串變量,並且效力低。
4. strTest.Equals(string.Empty) 不推舉應用,同 3。
5. strTest.Length == 0 這類方法,我不怎樣愛好用,不推舉應用。在本身的現實測試中,確切能證實這類斷定方法的履行效力最高,但要應用它你必需包管字符串不null,假如為null就會報出異常。
6. string.IsNullOrEmpty(strTest) 這類辦法是我最愛好用的,它不只一次機能斷定"空的字符串變量",還能斷定“值為空字符串的變量”,而且還可讓代碼簡練雅觀。斷定的效力也不算低。