欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2016年05月04日 11:47:53   作者:wenjunsu  
本文主要介紹了C#中判斷字符是否為空的方法,并實測對比各種方法的執(zhí)行效率,最后推薦大家使用IsNullOrEmpty,效率和易用性比較均衡。

C#中提供了相當(dāng)豐富的方法或?qū)傩詠砼袛嘁粋€字符是否為空,常用的方法有以下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; //定義開始時間 和 結(jié)束時間
    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種測試結(jié)果");     
      Console.WriteLine("=======================================");

      Test(strTest01);

      Console.WriteLine("=======================================");
      Console.WriteLine("strTest = string.Emtpy 的5種測試結(jié)果"); 
      Console.WriteLine("=======================================");

      Test(strTest02);

      Console.WriteLine("=======================================");
      Console.WriteLine("strTest = /"0123456789/" 的5種測試結(jié)果"); 
      Console.WriteLine("=======================================");

      Test(strTest03);

      Console.ReadLine();  //等待鍵盤的輸入 作用:使屏幕暫停在此處
    }
  }
}

我把能關(guān)的軟件都關(guān)閉掉了  盡可能的屏蔽掉系統(tǒng)影響  并且讓6種方法都運(yùn)行了1億次

第一次的截圖:

第一次測試結(jié)果

第二次的截圖: 

第二次測試結(jié)果

從以上可以看出:字符串在三種情況下,string.Length == 0的效率無疑是最高的。

總結(jié)

1. strTest== "" 不推薦使用,只能判斷“值為空字符串”的字符串變量,而且效率比較低。

2. strTest.Equals("") 不推薦使用,同 1。

3. strTest== string.Empty 不推薦使用,只能判斷“值為null”的字符串變量,而且效率低。

4. strTest.Equals(string.Empty) 不推薦使用,同 3。

5. strTest.Length == 0  這種方式,我不怎么喜歡用,不推薦使用。在自己的實際測試中,確實能證明這種判斷方式的執(zhí)行效率最高,但要使用它你必須保證字符串不null,如果為null就會報出異常。

6. string.IsNullOrEmpty(strTest)  這種方法是我最喜歡用的,它不但一次性能判斷"空的字符串變量",還能判斷“值為空字符串的變量”,并且還可以讓代碼簡潔美觀。判斷的效率也不算低。

相關(guān)文章

  • C#比較時間大小的方法總結(jié)

    C#比較時間大小的方法總結(jié)

    在本篇內(nèi)容里小編給大家分享的是關(guān)于C#比較時間大小的方法總結(jié),對此有需要的朋友們可以學(xué)習(xí)下。
    2018-12-12
  • c# 怎樣簡潔高效的實現(xiàn)多個 Enum 自由轉(zhuǎn)換

    c# 怎樣簡潔高效的實現(xiàn)多個 Enum 自由轉(zhuǎn)換

    這篇文章主要介紹了c# 怎樣簡潔高效的實現(xiàn)多個 Enum 自由轉(zhuǎn)換,幫助大家更好的進(jìn)行c# 開發(fā),感興趣的朋友可以了解下
    2020-10-10
  • 使用C#編寫自己的區(qū)塊鏈挖礦算法

    使用C#編寫自己的區(qū)塊鏈挖礦算法

    這篇文章主要介紹了使用C#編寫自己的區(qū)塊鏈挖礦算法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-08-08
  • C#數(shù)據(jù)庫操作的示例詳解

    C#數(shù)據(jù)庫操作的示例詳解

    這篇文章主要通過一些示例為大家詳細(xì)介紹了C#中數(shù)據(jù)庫操作用法,文中的示例代碼講解詳細(xì),具有有一定的借鑒價值,需要的可以參考一下
    2022-07-07
  • 基于C#實現(xiàn)壓縮和解壓文件及文件夾

    基于C#實現(xiàn)壓縮和解壓文件及文件夾

    這篇文章主要為大家詳細(xì)介紹了基于C#實現(xiàn)壓縮和解壓文件及文件夾的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-03-03
  • C#多線程用法詳解

    C#多線程用法詳解

    本文詳細(xì)講解了C#多線程用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12
  • AOP從靜態(tài)代理到動態(tài)代理(Emit實現(xiàn))詳解

    AOP從靜態(tài)代理到動態(tài)代理(Emit實現(xiàn))詳解

    AOP為Aspect Oriented Programming的縮寫,意思是面向切面編程的技術(shù)。下面這篇文章主要給大家介紹了關(guān)于AOP從靜態(tài)代理到動態(tài)代理(Emit實現(xiàn))的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-09-09
  • c#入門之循環(huán)語句使用詳解(for循環(huán)、do/while)

    c#入門之循環(huán)語句使用詳解(for循環(huán)、do/while)

    這篇文章主要介紹了c#入門之循環(huán)語句使用詳解,有for循環(huán)和do/while的示例,需要的朋友可以參考下
    2014-04-04
  • C#路徑,文件,目錄及IO常見操作匯總

    C#路徑,文件,目錄及IO常見操作匯總

    這篇文章主要介紹了C#路徑,文件,目錄及IO常見操作,較為詳細(xì)的分析并匯總了C#關(guān)于路徑,文件,目錄及IO常見操作,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09
  • C#判斷單詞個數(shù)方法總結(jié)

    C#判斷單詞個數(shù)方法總結(jié)

    我們給大家總計了C#中判斷英文單詞個數(shù)的方法以及排序的技巧,對此有需要的朋友可以測試下。
    2018-03-03

最新評論