国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C# 判斷字符為空的6種方法的效率實測對比

2020-01-24 01:10:32
字體:
來源:轉載
供稿:網友

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)  這種方法是我最喜歡用的,它不但一次性能判斷"空的字符串變量",還能判斷“值為空字符串的變量”,并且還可以讓代碼簡潔美觀。判斷的效率也不算低。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 遂平县| 达州市| 昌乐县| 个旧市| 双流县| 布尔津县| 安多县| 白城市| 大新县| 尼木县| 东宁县| 涟水县| 呈贡县| 和硕县| 巧家县| 陇川县| 西和县| 鱼台县| 年辖:市辖区| 松滋市| 尼勒克县| 正蓝旗| 日喀则市| 郸城县| 区。| 中牟县| 通城县| 汨罗市| 灵台县| 类乌齐县| 克什克腾旗| 当涂县| 丰镇市| 沛县| 万荣县| 温宿县| 鄂州市| 嵩明县| 永康市| 弋阳县| 安西县|