[VB] if 判断语句 和 If、IIf函数的比较

 Module Module1
Sub Main()
Dim i As Integer =
Dim s1 As String = "我是真的"
Dim s2 As String = "我不是真的"
Dim s3 As String
Dim mx As Integer =
Dim start As DateTime For iii As Integer = To
start = DateTime.Now
For ii As Integer = To mx
If i = Then
s3 = s1
Else
s3 = s2
End If
Next
Console.WriteLine((DateTime.Now - start).TotalMilliseconds) start = DateTime.Now
For ii As Integer = To mx
s3 = If(i = , s1, s2)
Next
Console.WriteLine((DateTime.Now - start).TotalMilliseconds) start = DateTime.Now
For ii As Integer = To mx
s3 = IIf(i = , s1, s2)
Next
Console.WriteLine((DateTime.Now - start).TotalMilliseconds) Console.WriteLine("--------------------------------")
Next
End Sub
End Module

执行结果:

2628.1503
2377.1359
6495.3716
--------------------------------
2602.1488
2390.1367
6535.3738
--------------------------------
2579.1475
2414.1381
6532.3736
--------------------------------
2651.1516
2405.1376
6627.3791
--------------------------------
2641.151
2390.1367
6538.374
--------------------------------
2619.1498
2398.1371
6656.3807
--------------------------------
2716.1554
2441.1396
6574.3761
--------------------------------
2623.15
2451.1402
6618.3786
--------------------------------
2650.1516
2409.1378
6550.3747
--------------------------------
2669.1527
2425.1387
6683.3823
--------------------------------

看结果很明显,效率上:If-Else 略大于 If(Express,SelectValue1,SelectValue2) 大于 IIf(Express,SelectValue1,SelectValue2)

这是为什么呢?

先看全部的IL(当然也可以略去不看,下面我已经将重要片段给截取出来做了比较,提示:本题中大部分指令已在本文结尾处做了注释)

 --- D:\Moontest\ConsoleApplication1\Module1.vb ---------------------------------
Dim i As Integer =
push ebp
mov ebp,esp
push edi
push esi
sub esp,108h
0000000b lea edi,[ebp+FFFFFEF0h]
mov ecx,40h
xor eax,eax
rep stos dword ptr es:[edi]
0000001a cmp dword ptr ds:[004B1538h],
je
call 682F477C
xor edx,edx
0000002a mov dword ptr [ebp+FFFFFF00h],edx
xor edx,edx
mov dword ptr [ebp+FFFFFF04h],edx
xor edx,edx
0000003a mov dword ptr [ebp+FFFFFEFCh],edx
xor edx,edx
mov dword ptr [ebp-10h],edx
xor edx,edx
mov dword ptr [ebp-0Ch],edx
0000004a xor edx,edx
0000004c mov dword ptr [ebp-24h],edx
0000004f xor edx,edx
mov dword ptr [ebp-20h],edx
xor edx,edx
mov dword ptr [ebp-1Ch],edx
xor edx,edx
0000005b mov dword ptr [ebp+FFFFFF6Ch],edx
xor edx,edx
mov dword ptr [ebp+FFFFFF68h],edx
xor edx,edx
0000006b mov dword ptr [ebp-28h],edx
0000006e mov dword ptr [ebp-0Ch],
Dim s1 As String = "我是真的"
mov eax,dword ptr ds:[033C2194h]
0000007b mov dword ptr [ebp+FFFFFF04h],eax
Dim s2 As String = "我不是真的"
mov eax,dword ptr ds:[033C2198h]
mov dword ptr [ebp+FFFFFF00h],eax
Dim s3 As String
Dim mx As Integer =
0000008d mov dword ptr [ebp-10h],3B9ACA00h
Dim start As DateTime 'For iii As Integer = 0 To 9
start = DateTime.Now
lea ecx,[ebp+FFFFFF60h]
0000009a call 6678EAC8
0000009f lea edi,[ebp-18h]
000000a2 lea esi,[ebp+FFFFFF60h]
000000a8 movq xmm0,mmword ptr [esi]
000000ac movq mmword ptr [edi],xmm0
For ii As Integer = To mx
000000b0 mov eax,dword ptr [ebp-10h]
000000b3 mov dword ptr [ebp-28h],eax
000000b6 xor edx,edx
000000b8 mov dword ptr [ebp-1Ch],edx
000000bb nop
000000bc jmp 000000EA
If i = Then
000000be cmp dword ptr [ebp-0Ch],
000000c2 jne 000000D3
s3 = s1
000000c4 mov eax,dword ptr [ebp+FFFFFF04h]
000000ca mov dword ptr [ebp+FFFFFEFCh],eax
000000d0 nop
000000d1 jmp 000000DF
Else
s3 = s2
000000d3 mov eax,dword ptr [ebp+FFFFFF00h]
000000d9 mov dword ptr [ebp+FFFFFEFCh],eax
End If
Next
000000df add dword ptr [ebp-1Ch],
000000e3 jno 000000EA
000000e5 call 682F3C1A
000000ea mov eax,dword ptr [ebp-1Ch]
000000ed cmp eax,dword ptr [ebp-28h]
000000f0 jle 000000BE
Console.WriteLine((DateTime.Now - start).TotalMilliseconds)
000000f2 lea ecx,[ebp+FFFFFF58h]
000000f8 call 6678EAC8
000000fd lea eax,[ebp+FFFFFF58h]
sub esp,
movq xmm0,mmword ptr [eax]
0000010a movq mmword ptr [esp],xmm0
0000010f lea eax,[ebp-18h]
sub esp,
movq xmm0,mmword ptr [eax]
movq mmword ptr [esp],xmm0
0000011e lea ecx,[ebp+FFFFFF50h]
call 6678A950
lea edi,[ebp+FFFFFF70h]
0000012f lea esi,[ebp+FFFFFF50h]
movq xmm0,mmword ptr [esi]
movq mmword ptr [edi],xmm0
0000013d lea ecx,[ebp+FFFFFF70h]
call 6678DD38
fstp qword ptr [ebp+FFFFFF48h]
0000014e fld qword ptr [ebp+FFFFFF48h]
sub esp,
fstp qword ptr [esp]
0000015a call 66E72E20 start = DateTime.Now
0000015f lea ecx,[ebp+FFFFFF40h]
call 6678EAC8
0000016a lea edi,[ebp-18h]
0000016d lea esi,[ebp+FFFFFF40h]
movq xmm0,mmword ptr [esi]
movq mmword ptr [edi],xmm0
For ii As Integer = To mx
0000017b mov eax,dword ptr [ebp-10h]
0000017e mov dword ptr [ebp+FFFFFF6Ch],eax
xor edx,edx
mov dword ptr [ebp-20h],edx
nop
0000018a jmp 000001C4
s3 = If(i = , s1, s2)
0000018c cmp dword ptr [ebp-0Ch],
je 000001A1
nop
mov eax,dword ptr [ebp+FFFFFF00h]
mov dword ptr [ebp+FFFFFEF8h],eax
0000019f jmp 000001AD
000001a1 mov eax,dword ptr [ebp+FFFFFF04h]
000001a7 mov dword ptr [ebp+FFFFFEF8h],eax
000001ad mov eax,dword ptr [ebp+FFFFFEF8h]
000001b3 mov dword ptr [ebp+FFFFFEFCh],eax
Next
000001b9 add dword ptr [ebp-20h],
000001bd jno 000001C4
000001bf call 682F3C1A
000001c4 mov eax,dword ptr [ebp-20h]
000001c7 cmp eax,dword ptr [ebp+FFFFFF6Ch]
000001cd jle 0000018C
Console.WriteLine((DateTime.Now - start).TotalMilliseconds)
000001cf lea ecx,[ebp+FFFFFF38h]
000001d5 call 6678EAC8
000001da lea eax,[ebp+FFFFFF38h]
000001e0 sub esp,
000001e3 movq xmm0,mmword ptr [eax]
000001e7 movq mmword ptr [esp],xmm0
000001ec lea eax,[ebp-18h]
000001ef sub esp,
000001f2 movq xmm0,mmword ptr [eax]
000001f6 movq mmword ptr [esp],xmm0
000001fb lea ecx,[ebp+FFFFFF30h]
call 6678A950
lea edi,[ebp-30h]
lea esi,[ebp+FFFFFF30h]
0000020f movq xmm0,mmword ptr [esi]
movq mmword ptr [edi],xmm0
lea ecx,[ebp-30h]
0000021a call 6678DD38
0000021f fstp qword ptr [ebp+FFFFFF28h]
fld qword ptr [ebp+FFFFFF28h]
0000022b sub esp,
0000022e fstp qword ptr [esp]
call 66E72E20 start = DateTime.Now
lea ecx,[ebp+FFFFFF20h]
0000023c call 6678EAC8
lea edi,[ebp-18h]
lea esi,[ebp+FFFFFF20h]
0000024a movq xmm0,mmword ptr [esi]
0000024e movq mmword ptr [edi],xmm0
For ii As Integer = To mx
mov eax,dword ptr [ebp-10h]
mov dword ptr [ebp+FFFFFF68h],eax
0000025b xor edx,edx
0000025d mov dword ptr [ebp-24h],edx
nop
jmp 000002AC
s3 = IIf(i = , s1, s2)
push dword ptr [ebp+FFFFFF00h]
cmp dword ptr [ebp-0Ch],
0000026d sete cl
movzx ecx,cl
mov edx,dword ptr [ebp+FFFFFF04h]
call 55FD3238
0000027e mov dword ptr [ebp+FFFFFEF4h],eax
mov ecx,dword ptr [ebp+FFFFFEF4h]
0000028a call 55FAFAA0
0000028f mov dword ptr [ebp+FFFFFEF0h],eax
mov eax,dword ptr [ebp+FFFFFEF0h]
0000029b mov dword ptr [ebp+FFFFFEFCh],eax
Next
000002a1 add dword ptr [ebp-24h],
000002a5 jno 000002AC
000002a7 call 682F3C1A
000002ac mov eax,dword ptr [ebp-24h]
000002af cmp eax,dword ptr [ebp+FFFFFF68h]
000002b5 jle
Console.WriteLine((DateTime.Now - start).TotalMilliseconds)
000002b7 lea ecx,[ebp+FFFFFF18h]
000002bd call 6678EAC8
000002c2 lea eax,[ebp+FFFFFF18h]
000002c8 sub esp,
000002cb movq xmm0,mmword ptr [eax]
000002cf movq mmword ptr [esp],xmm0
000002d4 lea eax,[ebp-18h]
000002d7 sub esp,
000002da movq xmm0,mmword ptr [eax]
000002de movq mmword ptr [esp],xmm0
000002e3 lea ecx,[ebp+FFFFFF10h]
000002e9 call 6678A950
000002ee lea edi,[ebp-30h]
000002f1 lea esi,[ebp+FFFFFF10h]
000002f7 movq xmm0,mmword ptr [esi]
000002fb movq mmword ptr [edi],xmm0
000002ff lea ecx,[ebp-30h]
call 6678DD38
fstp qword ptr [ebp+FFFFFF08h]
0000030d fld qword ptr [ebp+FFFFFF08h]
sub esp,
fstp qword ptr [esp]
call 66E72E20 Console.WriteLine("--------------------------------")
0000031e mov ecx,dword ptr ds:[033C219Ch]
call 6682AAB8
'Next
End Sub
nop
0000032a lea esp,[ebp-]
0000032d pop esi
0000032e pop edi
0000032f pop ebp
ret

转化IL

对比不同点(If-Else || If(Express,SelecetValue1,SelectValue2) || IIf(Express,SelecetValue1,SelectValue2):

[VB] if 判断语句 和 If、IIf函数的比较

红色方框内的行数,影响到运行时间。

看后我们发现,If-Else 与 If(Express,SelectValue1,SelectValue2) 实现的IL代码几乎完全一样( If(Express,SelectValue1,SelectValue2) IL中虽然多执行了两步操作,但是仔细看你会发现只不过是进行了多余操作)。

那么我们在执行过程时,为什么If-Else的时间略比If(Express,SelectValue1,SelectValue2)的时间大呢? 那是因为If-Else里面多了nop 指令,如果没它,If-Else 比 If(Express,SelectValue1,SelectValue2) 快。

至于 IIf(Express,SelectValue1,SelectValue2)  那是因为里面多进行了两步call 指令,这两个过程是执行内存别处指令,执行完之后再跳转回执行下一条指令(应该是Iff有很多判断,比较复杂吧?!我没用有仔细查看内存)。

[VB] if 判断语句 和 If、IIf函数的比较

[VB] if 判断语句 和 If、IIf函数的比较

帮助信息(参阅 IL指令汇总):

dword ptr[ebp-0Ch]  :  取内存地址ebp-0Ch中的数据,地址ebp-0Ch中保存的是Integer i

cmp  dword ptr[ebp-0Ch]  ,   0   : 比较 i 和 0 【对两数进行相减,进行比较,不将两数的差放入第一个操作数,影响标志寄存器,个人认为应该影响零标识(ZF)和符号标识(SF),具体的请搜索下标志寄存器】

jne  000000d3  :    ZF=0 , 跳转至  000000d3  (ZF是零标识,若运算结果为零则ZF=1,否则ZF=0)

mov eax , dword ptr[ebp+FFFFFF04h] :  将操作数dword ptr[ebp+FFFFFF04h]送入eax寄存器 , 这个是什么玩意呢?  此句中的dword ptr[ebp+FFFFFF04h] 指向我们自定义的字符串

jmp 000000DF : 强制跳转至 000000DF

je 000001A1 :  ZF=1,跳转至  000001A1 (参考jne)

sete cl : 取标志寄存器中ZF的值, 放到cl中

call  55EC3238  :  调至55EC3238执行指令

movzx  ecx , cl : ecx 高位8~32位强制为0

nop :本指令不产生任何结果,仅消耗几个时钟周期的时间,接着执行后续指令,常用于程序的延时等

上一篇:C++之路进阶——codevs1362(网络扩容)


下一篇:Android开发系列(十八):自己定义控件样式在drawable目录下的XML实现