由浅入深CIL系列:5.抛砖引玉:判断string是否为空的四种方法的CIL代码看看效率如何?...
????????????? ldc.i4.0????將整數(shù)值 0 作為 int32 推送到計(jì)算堆棧上
????????????? Ceq???????? 比較兩個(gè)值。如果這兩個(gè)值相等,則將整數(shù)值 1 (int32) 推送到計(jì)算堆棧上;否則,將 0 (int32) 推送到計(jì)算堆棧上。
????????????? Brtrue.s?? 如果 value 為 true、非空或非零,則將控制轉(zhuǎn)移到目標(biāo)指令(短格式)。
????????????? Brfalse.S ?如果 value 為 false、空引用或零,則將控制轉(zhuǎn)移到目標(biāo)指令。
????????????? Callvirt???? 對(duì)對(duì)象調(diào)用后期綁定方法,并且將返回值推送到計(jì)算堆棧上。
????????????? Ldsfld??????將靜態(tài)字段的值推送到計(jì)算堆棧上。
源代碼
??????? 一、在.NET有幾種判斷string是否為空的方法,也有兩種判斷值是否相等的方法。下面我們來(lái)看看:
class Program{
static void Main(string[] args)
{
//判斷字符串是否為空
string str1 = "MyWord";
if (str1 == "")
;
if (str1 == string.Empty)
;
??????????? if (str1 != null && str1.Length== 0)
??????????????? ;
??????????? if (string.IsNullOrEmpty(str1))
??????????????? ;?? }
} 二、下面我們看看上面的Cs代碼生成的CIL代碼如下: .method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// 代碼大小 85 (0x55)
.maxstack 2
//聲明3個(gè)參數(shù),分別是str1和bool值
.locals init ([0] string str1,
[1] bool CS$4$0000)
IL_0000: nop
//推送對(duì)元數(shù)據(jù)中存儲(chǔ)的"MyWord"字符串的新對(duì)象引用
IL_0001: ldstr "MyWord"
//將"MyWord"壓棧到參數(shù)0
IL_0006: stloc.0? ?
?//--------string空判斷第一種方法 if (str1 == "") --------
//將"MyWord"從參數(shù)0處加載到計(jì)算堆棧上
IL_0007: ldloc.0
//推送對(duì)元數(shù)據(jù)中存儲(chǔ)的""字符串的新對(duì)象引用
IL_0008: ldstr ""
//通過(guò)System.String::op_Equality函數(shù)判斷是否相等
IL_000d: call bool [mscorlib]System.String::op_Equality(string,
string)
//將整數(shù)值 0 作為 int32 推送到計(jì)算堆棧上
IL_0012: ldc.i4.0
//ceq比較兩個(gè)值。如果這兩個(gè)值相等,則將整數(shù)值 1 (int32)推送到計(jì)算堆棧上;
//否則,將 0 (int32) 推送到計(jì)算堆棧上。
IL_0013: ceq
//將true或者false的bool值彈出棧存到參數(shù)1去
IL_0015: stloc.1
//從參數(shù)1中加載數(shù)據(jù)到計(jì)算堆棧上去
IL_0016: ldloc.1
//如果 value 為 true、非空或非零,則將控制轉(zhuǎn)移到目標(biāo)指令(短格式)。
//也就是if判斷中如果結(jié)果為true的話(huà),則運(yùn)行內(nèi)部代碼
IL_0017: brtrue.s IL_0019
?//--------string空判斷第二種方法 if (str1 == string.Empty) --------
IL_0019: ldloc.0
//Ldsfld 將靜態(tài)字段的值推送到計(jì)算堆棧上。
IL_001a: ldsfld string [mscorlib]System.String::Empty
IL_001f: call bool [mscorlib]System.String::op_Equality(string,
string)
IL_0024: ldc.i4.0
IL_0025: ceq
IL_0027: stloc.1
IL_0028: ldloc.1
IL_0029: brtrue.s IL_002b
?//--------string空判斷第三種方法 if (str1!=null&&str1.Length == 0) --------
IL_002b: ldloc.0
//對(duì)象調(diào)用后期綁定方法,并且將返回值推送到計(jì)算堆棧上。<==> str1!=null
IL_002c: brfalse.s IL_003c
IL_002e: ldloc.0
//調(diào)用系統(tǒng)函數(shù)獲取長(zhǎng)度
IL_002f: callvirt instance int32 [mscorlib]System.String::get_Length()
IL_0034: ldc.i4.0
IL_0035: ceq
IL_0037: ldc.i4.0
IL_0038: ceq
IL_003a: br.s IL_003d
IL_003c: ldc.i4.1
IL_003d: stloc.1
IL_003e: ldloc.1
IL_003f: brtrue.s IL_0041
//--------string空判斷第四種方法 if (string.IsNullOrEmpty(str1)) --------
IL_0041: ldloc.0
//直接調(diào)用系統(tǒng)System.String::IsNullOrEmpty(string)函數(shù)比對(duì)
IL_0042: call bool [mscorlib]System.String::IsNullOrEmpty(string)
IL_0047: ldc.i4.0
IL_0048: ceq
IL_004a: stloc.1
IL_004b: ldloc.1
IL_004c: brtrue.s IL_004e
} // end of method Program::Main
?4種方法的CIL分析
??????????????A.if (str1 == ""),在這里我們需要新構(gòu)造一個(gè)""空字符,然后再調(diào)用System.String::op_Equality(string,string)函數(shù)對(duì)str1和空字符進(jìn)行對(duì)比。
//--------string空判斷第一種方法 if (str1 == "") --------//將"MyWord"從參數(shù)0處加載到計(jì)算堆棧上
IL_0007: ldloc.0
//推送對(duì)元數(shù)據(jù)中存儲(chǔ)的""字符串的新對(duì)象引用
IL_0008: ldstr ""
//通過(guò)System.String::op_Equality函數(shù)判斷是否相等
IL_000d: call bool [mscorlib]System.String::op_Equality(string,
string)
//將整數(shù)值 0 作為 int32 推送到計(jì)算堆棧上
IL_0012: ldc.i4.0
//ceq比較兩個(gè)值。如果這兩個(gè)值相等,則將整數(shù)值 1 (int32)推送到計(jì)算堆棧上;
//否則,將 0 (int32) 推送到計(jì)算堆棧上。
IL_0013: ceq
//將true或者false的bool值彈出棧存到參數(shù)1去
IL_0015: stloc.1
//從參數(shù)1中加載數(shù)據(jù)到計(jì)算堆棧上去
IL_0016: ldloc.1
//如果 value 為 true、非空或非零,則將控制轉(zhuǎn)移到目標(biāo)指令(短格式)。
//也就是if判斷中如果結(jié)果為true的話(huà),則運(yùn)行內(nèi)部代碼
IL_0017: brtrue.s IL_0019 B.if (str1 == string.Empty),在這里我們通過(guò)string [mscorlib]System.String::Empty加載一個(gè)CIL代碼為.field public static initonly string Empty的靜態(tài)字段,然后讓str1和這個(gè)靜態(tài)字段做比較System.String::op_Equality(string,string),以確定是否為空。
//--------string空判斷第二種方法 if (str1 == string.Empty) --------
IL_0019: ldloc.0
//Ldsfld 將靜態(tài)字段的值推送到計(jì)算堆棧上。
IL_001a: ldsfld string [mscorlib]System.String::Empty
IL_001f: call bool [mscorlib]System.String::op_Equality(string,
string)
IL_0024: ldc.i4.0
IL_0025: ceq
IL_0027: stloc.1
IL_0028: ldloc.1
IL_0029: brtrue.s IL_002b C.if (str1.Length == 0),在這里我們調(diào)用[mscorlib]System.String::get_Length()函數(shù)獲取到字符串長(zhǎng)度,然后這個(gè)長(zhǎng)度和0相對(duì)比 //--------string空判斷第三種方法 if (str1!=null&&str1.Length == 0) --------
IL_002b: ldloc.0
//對(duì)象調(diào)用后期綁定方法,并且將返回值推送到計(jì)算堆棧上。<==> str1!=null
IL_002c: brfalse.s IL_003c
IL_002e: ldloc.0
//調(diào)用系統(tǒng)函數(shù)獲取長(zhǎng)度
IL_002f: callvirt instance int32 [mscorlib]System.String::get_Length()
IL_0034: ldc.i4.0
IL_0035: ceq
IL_0037: ldc.i4.0
IL_0038: ceq
IL_003a: br.s IL_003d
IL_003c: ldc.i4.1
IL_003d: stloc.1
IL_003e: ldloc.1
IL_003f: brtrue.s IL_0041
?????????????? D.if (string.IsNullOrEmpty(str1)),這種方式直接調(diào)用系統(tǒng)的System.String::IsNullOrEmpty(string)函數(shù)直接比對(duì)出結(jié)果。
//--------string空判斷第四種方法 if (string.IsNullOrEmpty(str1)) --------IL_0041: ldloc.0
//直接調(diào)用系統(tǒng)System.String::IsNullOrEmpty(string)函數(shù)比對(duì)
IL_0042: call bool [mscorlib]System.String::IsNullOrEmpty(string)
IL_0047: ldc.i4.0
IL_0048: ceq
IL_004a: stloc.1
IL_004b: ldloc.1
IL_004c: brtrue.s IL_004e
???????
性能分析
??????? 下面我們通過(guò)using System.Diagnostics;命名空間下的Stopwatch對(duì)象來(lái)計(jì)算這4種調(diào)用方式所消耗的大概時(shí)間。
請(qǐng)看cs代碼如下:
class Program{
static void Main(string[] args)
{
//判斷字符串是否為空
string str1 = "MyWord";
//第一種方法耗時(shí)計(jì)算
Stopwatch sw1 = new Stopwatch();
sw1.Start();
if (str1 == "")
;
sw1.Stop();
//第二種方法耗時(shí)計(jì)算
Stopwatch sw2 = new Stopwatch();
sw2.Start();
if (str1 == string.Empty)
;
sw2.Stop();
//第三種方法耗時(shí)計(jì)算
Stopwatch sw3 = new Stopwatch();
sw3.Start();
if (str1!=null&&str1.Length == 0)
;
sw3.Stop();
//第四種方法耗時(shí)計(jì)算
Stopwatch sw4 = new Stopwatch();
sw4.Start();
if (string.IsNullOrEmpty(str1))
;
sw4.Stop();
Console.WriteLine(@"if (str1 == "")的判斷時(shí)間是:" + sw1.Elapsed);
Console.WriteLine(@"if (str1 == string.Empty)的判斷時(shí)間是:" + sw2.Elapsed);
Console.WriteLine(@"if (str1!=null&&str1.Length == 0)的判斷時(shí)間是:" + sw3.Elapsed);
Console.WriteLine(@"if (string.IsNullOrEmpty(str1)) 的判斷時(shí)間是:" + sw4.Elapsed);
Console.ReadLine();
}
????????然后我們需要看看結(jié)果如何,為了提高精確度,我們運(yùn)行多次結(jié)果,然后就知道哪種方式的效率最高。
下面我們來(lái)看在我的電腦上的運(yùn)行時(shí)間情況如下面的圖所示:
?
?
?????? 然后我將這段代碼發(fā)我一個(gè)朋友那里得到的運(yùn)行情況如下圖所示:
??????? 鑒于時(shí)間跨度太小,以及各種運(yùn)行環(huán)境的不同,還有其他一些原因,對(duì)于結(jié)果和答案都有有所影響,所以上面的運(yùn)行結(jié)果僅做參考。大家也可以將這段測(cè)試代碼在自己的電腦上運(yùn)行一下,看看究竟結(jié)果如何?
思考:這4種方法的效率究竟誰(shuí)高誰(shuí)低?應(yīng)該如何排序?為什么形成這樣的差異?
??
擴(kuò)展閱讀:
?????? I.1第一種方法和第二種方法都會(huì)使用到一個(gè)System.String::op_Equality(string,string)方法,這個(gè)方法的CIL代碼我們使用ILDASM查看mscorlib.dll文件即可:
System.String::op_Equality(string,string) .method public hidebysig specialname staticbool op_Equality(string a,
string b) cil managed
{
// 代碼大小 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: call bool System.String::Equals(string,
string)
IL_0007: ret
} // end of method String::op_Equality
??????? I.2上面這段IL代碼內(nèi)部調(diào)用了bool System.String::Equals(string,string)方法,這個(gè)方法的CIL代碼如下:
bool System.String::Equals(string,string) .method public hidebysig static bool Equals(string a,string b) cil managed
{
// 代碼大小 22 (0x16)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: bne.un.s IL_0006
IL_0004: ldc.i4.1
IL_0005: ret
IL_0006: ldarg.0
IL_0007: brfalse.s IL_000c
IL_0009: ldarg.1
IL_000a: brtrue.s IL_000e
IL_000c: ldc.i4.0
IL_000d: ret
IL_000e: ldarg.0
IL_000f: ldarg.1
IL_0010: call bool System.String::EqualsHelper(string,
string)
IL_0015: ret
} // end of method String::Equals ??
?????? I.3上面這段IL代碼內(nèi)部調(diào)用了bool System.String::EqualsHelper(string,?string) 方法,這個(gè)方法的CIL代碼如下,其內(nèi)部調(diào)用了多次int32 System.String::get_Length()函數(shù):??
System.String::EqualsHelper(string,string) .method private hidebysig static bool EqualsHelper(string strA,string strB) cil managed
{
.custom instance void System.Security.SecuritySafeCriticalAttribute::.ctor() = ( 01 00 00 00 )
.custom instance void System.Runtime.ConstrainedExecution.ReliabilityContractAttribute::.ctor(valuetype System.Runtime.ConstrainedExecution.Consistency,
valuetype System.Runtime.ConstrainedExecution.Cer) = ( 01 00 03 00 00 00 01 00 00 00 00 00 )
// 代碼大小 199 (0xc7)
.maxstack 3
.locals init (int32 V_0,
char& pinned V_1,
char& pinned V_2,
char* V_3,
char* V_4,
bool V_5)
IL_0000: ldarg.0
IL_0001: callvirt instance int32 System.String::get_Length()
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: ldarg.1
IL_0009: callvirt instance int32 System.String::get_Length()
IL_000e: beq.s IL_0012
IL_0010: ldc.i4.0
IL_0011: ret
IL_0012: ldarg.0
IL_0013: ldflda char System.String::m_firstChar
IL_0018: stloc.1
IL_0019: ldarg.1
IL_001a: ldflda char System.String::m_firstChar
IL_001f: stloc.2
IL_0020: ldloc.1
IL_0021: conv.i
IL_0022: stloc.3
IL_0023: ldloc.2
IL_0024: conv.i
IL_0025: stloc.s V_4
IL_0027: br.s IL_0097
IL_0029: ldloc.3
IL_002a: ldind.i4
IL_002b: ldloc.s V_4
IL_002d: ldind.i4
IL_002e: beq.s IL_0038
IL_0030: ldc.i4.0
IL_0031: stloc.s V_5
IL_0033: leave IL_00c4
IL_0038: ldloc.3
IL_0039: ldc.i4.4
IL_003a: conv.i
IL_003b: add
IL_003c: ldind.i4
IL_003d: ldloc.s V_4
IL_003f: ldc.i4.4
IL_0040: conv.i
IL_0041: add
IL_0042: ldind.i4
IL_0043: beq.s IL_004a
IL_0045: ldc.i4.0
IL_0046: stloc.s V_5
IL_0048: leave.s IL_00c4
IL_004a: ldloc.3
IL_004b: ldc.i4.8
IL_004c: conv.i
IL_004d: add
IL_004e: ldind.i4
IL_004f: ldloc.s V_4
IL_0051: ldc.i4.8
IL_0052: conv.i
IL_0053: add
IL_0054: ldind.i4
IL_0055: beq.s IL_005c
IL_0057: ldc.i4.0
IL_0058: stloc.s V_5
IL_005a: leave.s IL_00c4
IL_005c: ldloc.3
IL_005d: ldc.i4.s 12
IL_005f: conv.i
IL_0060: add
IL_0061: ldind.i4
IL_0062: ldloc.s V_4
IL_0064: ldc.i4.s 12
IL_0066: conv.i
IL_0067: add
IL_0068: ldind.i4
IL_0069: beq.s IL_0070
IL_006b: ldc.i4.0
IL_006c: stloc.s V_5
IL_006e: leave.s IL_00c4
IL_0070: ldloc.3
IL_0071: ldc.i4.s 16
IL_0073: conv.i
IL_0074: add
IL_0075: ldind.i4
IL_0076: ldloc.s V_4
IL_0078: ldc.i4.s 16
IL_007a: conv.i
IL_007b: add
IL_007c: ldind.i4
IL_007d: beq.s IL_0084
IL_007f: ldc.i4.0
IL_0080: stloc.s V_5
IL_0082: leave.s IL_00c4
IL_0084: ldloc.3
IL_0085: ldc.i4.s 20
IL_0087: conv.i
IL_0088: add
IL_0089: stloc.3
IL_008a: ldloc.s V_4
IL_008c: ldc.i4.s 20
IL_008e: conv.i
IL_008f: add
IL_0090: stloc.s V_4
IL_0092: ldloc.0
IL_0093: ldc.i4.s 10
IL_0095: sub
IL_0096: stloc.0
IL_0097: ldloc.0
IL_0098: ldc.i4.s 10
IL_009a: bge.s IL_0029
IL_009c: br.s IL_00b5
IL_009e: ldloc.3
IL_009f: ldind.i4
IL_00a0: ldloc.s V_4
IL_00a2: ldind.i4
IL_00a3: bne.un.s IL_00b9
IL_00a5: ldloc.3
IL_00a6: ldc.i4.4
IL_00a7: conv.i
IL_00a8: add
IL_00a9: stloc.3
IL_00aa: ldloc.s V_4
IL_00ac: ldc.i4.4
IL_00ad: conv.i
IL_00ae: add
IL_00af: stloc.s V_4
IL_00b1: ldloc.0
IL_00b2: ldc.i4.2
IL_00b3: sub
IL_00b4: stloc.0
IL_00b5: ldloc.0
IL_00b6: ldc.i4.0
IL_00b7: bgt.s IL_009e
IL_00b9: ldloc.0
IL_00ba: ldc.i4.0
IL_00bb: cgt
IL_00bd: ldc.i4.0
IL_00be: ceq
IL_00c0: stloc.s V_5
IL_00c2: leave.s IL_00c4
IL_00c4: ldloc.s V_5
IL_00c6: ret
} // end of method String::EqualsHelper
??????? II.1在第三種方法的CIL代碼中我們調(diào)用了一次int32 [mscorlib]System.String::get_Length()函數(shù).
??????? III.1在第四種方法的CIL代碼中調(diào)用了一次bool [mscorlib]System.String::IsNullOrEmpty(string)函數(shù),此函數(shù)的CIL代碼如下,它內(nèi)部調(diào)用了一次System.String::get_Length()函數(shù):
System.String::IsNullOrEmpty(string) .method public hidebysig static bool IsNullOrEmpty(string 'value') cil managed{
// 代碼大小 15 (0xf)
.maxstack 8
IL_0000: ldarg.0
IL_0001: brfalse.s IL_000d
IL_0003: ldarg.0
IL_0004: callvirt instance int32 System.String::get_Length()
IL_0009: ldc.i4.0
IL_000a: ceq
IL_000c: ret
IL_000d: ldc.i4.1
IL_000e: ret
} // end of method String::IsNullOrEmpty ?
總結(jié)
以上是生活随笔為你收集整理的由浅入深CIL系列:5.抛砖引玉:判断string是否为空的四种方法的CIL代码看看效率如何?...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 梦到房子被别人住了是怎么回事
- 下一篇: 梦到儿子病了是什么征兆