C# 判斷字符為空的6種方法的效率實(shí)測(cè)對(duì)比
C#中提供了相當(dāng)豐富的方法或?qū)傩詠?lái)判斷一個(gè)字符是否為空,常用的方法有以下6種
1. strTest== ""
2. strTest.Equals("")
3. strTest== string.Empty
4. strTest.Equals(string.Empty)
5. strTest.Length == 0
6. string.IsNullOrEmpty(strTest)
為了對(duì)以上6種方法的效率,有個(gè)直觀的感受,我特意編寫(xiě)了以下的測(cè)試代碼:
using System;
namespace StrTest
{
class Program
{
//定義3個(gè)字符串 以便測(cè)試在多種情況下 下面6種判斷方法的速度
public static string strTest01 = "";
public static string strTest02 = string.Empty;
public static string strTest03 = "0123456789";
public static DateTime start, end; //定義開(kāi)始時(shí)間 和 結(jié)束時(shí)間
public static TimeSpan ts; //定義兩個(gè)時(shí)間的間隔
//**********************對(duì)strTest使用6種測(cè)試方法*****************************
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 == /"/" 時(shí)間消耗為 " + 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(/"/") 時(shí)間消耗為 " + 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 時(shí)間消耗為 " + 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) 時(shí)間消耗為 " + 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 時(shí)間消耗為 " + 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) 時(shí)間消耗為 " + ts.TotalSeconds + " 秒" + "/n");
}
static void Main(string[] args)
{
Console.WriteLine("=======================================");
Console.WriteLine("strTest = /"/" 的5種測(cè)試結(jié)果");
Console.WriteLine("=======================================");
Test(strTest01);
Console.WriteLine("=======================================");
Console.WriteLine("strTest = string.Emtpy 的5種測(cè)試結(jié)果");
Console.WriteLine("=======================================");
Test(strTest02);
Console.WriteLine("=======================================");
Console.WriteLine("strTest = /"0123456789/" 的5種測(cè)試結(jié)果");
Console.WriteLine("=======================================");
Test(strTest03);
Console.ReadLine(); //等待鍵盤(pán)的輸入 作用:使屏幕暫停在此處
}
}
}
我把能關(guān)的軟件都關(guān)閉掉了 盡可能的屏蔽掉系統(tǒng)影響 并且讓6種方法都運(yùn)行了1億次
第一次的截圖:

第二次的截圖:

從以上可以看出:字符串在三種情況下,string.Length == 0的效率無(wú)疑是最高的。
總結(jié)
1. strTest== "" 不推薦使用,只能判斷“值為空字符串”的字符串變量,而且效率比較低。
2. strTest.Equals("") 不推薦使用,同 1。
3. strTest== string.Empty 不推薦使用,只能判斷“值為null”的字符串變量,而且效率低。
4. strTest.Equals(string.Empty) 不推薦使用,同 3。
5. strTest.Length == 0 這種方式,我不怎么喜歡用,不推薦使用。在自己的實(shí)際測(cè)試中,確實(shí)能證明這種判斷方式的執(zhí)行效率最高,但要使用它你必須保證字符串不null,如果為null就會(huì)報(bào)出異常。
6. string.IsNullOrEmpty(strTest) 這種方法是我最喜歡用的,它不但一次性能判斷"空的字符串變量",還能判斷“值為空字符串的變量”,并且還可以讓代碼簡(jiǎn)潔美觀。判斷的效率也不算低。
相關(guān)文章
C#使用CancellationTokenSource 取消 Task的方法
因?yàn)樯婕暗搅巳粘=?jīng)常會(huì)碰到的取消任務(wù)操作,本文主要介紹了C#使用CancellationTokenSource 取消 Task,文中通過(guò)代碼介紹的非常詳細(xì),感興趣的可以了解一下2022-02-02
C#基礎(chǔ)知識(shí)系列八const和readonly關(guān)鍵字詳細(xì)介紹
這篇文章主要介紹了C#中的const和readonly關(guān)鍵字,有需要的朋友可以參考一下2014-01-01
使用C#編寫(xiě)簡(jiǎn)單的圖形化的可發(fā)送附件的郵件客戶端程序
這篇文章主要介紹了使用C#編寫(xiě)一個(gè)圖形化的可發(fā)送附件的郵件客戶端程序的方法,文中的示例同樣是基于支持smtp協(xié)議的郵件服務(wù)器,需要的朋友可以參考下2016-02-02
c#中的virtual方法及應(yīng)用場(chǎng)景分析
在 C# 中,virtual?關(guān)鍵字用于修飾方法、屬性、索引器或事件,這篇文章主要介紹了c#中的virtual方法及應(yīng)用場(chǎng)景分析,需要的朋友可以參考下2025-03-03

