C# 判斷字符為空的6種方法的效率實(shí)測(cè)對(duì)比
C#中提供了相當(dāng)豐富的方法或?qū)傩詠砼袛嘁粋€(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è)直觀的感受,我特意編寫了以下的測(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; //定義開始時(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(); //等待鍵盤的輸入 作用:使屏幕暫停在此處 } } }
我把能關(guān)的軟件都關(guān)閉掉了 盡可能的屏蔽掉系統(tǒng)影響 并且讓6種方法都運(yùn)行了1億次
第一次的截圖:
第二次的截圖:
從以上可以看出:字符串在三種情況下,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 這種方式,我不怎么喜歡用,不推薦使用。在自己的實(shí)際測(cè)試中,確實(shí)能證明這種判斷方式的執(zhí)行效率最高,但要使用它你必須保證字符串不null,如果為null就會(huì)報(bào)出異常。
6. string.IsNullOrEmpty(strTest) 這種方法是我最喜歡用的,它不但一次性能判斷"空的字符串變量",還能判斷“值為空字符串的變量”,并且還可以讓代碼簡(jiǎn)潔美觀。判斷的效率也不算低。
欄 目:C#教程
下一篇:C#重寫DataGridView
本文標(biāo)題:C# 判斷字符為空的6種方法的效率實(shí)測(cè)對(duì)比
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6548.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)實(shí)體類與字符串互相轉(zhuǎn)換的方法
- 01-10C#實(shí)現(xiàn)判斷當(dāng)前操作用戶管理角色的方法
- 01-10WinForm判斷關(guān)閉事件來源于用戶點(diǎn)擊右上角“關(guān)閉”按鈕的方法
- 01-10C# readnodefile()不能讀取帶有文件名為漢字的osg文件解決方法
- 01-10C#實(shí)現(xiàn)將應(yīng)用程序設(shè)置為開機(jī)啟動(dòng)的方法
- 01-10使用Nopcommerce為商城添加滿XX減XX優(yōu)惠券功能
- 01-10http圖片上傳安全性問題 根據(jù)ContentType (MIME) 判斷其實(shí)不準(zhǔn)確、不
- 01-10C#中Json字符串的各種應(yīng)用類實(shí)例講解
- 01-10VS中C#讀取app.config數(shù)據(jù)庫配置字符串的三種方法
- 01-10C#實(shí)現(xiàn)過濾sql特殊字符的方法集合


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?