3

我对 VB.NET 比较陌生,并且是自学编程的,所以我没有很多基础知识和概念,所以请原谅我的天真和无知。

我对逐步或仅在一行代码中编写 VB 函数的后勤、性能和效率感到好奇。

以下是我的一个程序中的一个小函数,用于解析一些 HTML。请注意,代码只是一个随机示例,两个代码块执行完全相同的功能。它们在这里分别与一段长而简洁的代码相比,说明了我的长、程序声明、分段等代码的观点。

  1. 长码

    Dim html As String
    Dim htmlString As String
    Dim dIndex As Integer
    html = WebBrowser.DocumentText
    htmlString = "size=" & Chr(34) & "15" & Chr(34) & " maxlength=" & Chr(34) & "40" & Chr(34) & ">"
    dIndex = html.IndexOf(htmlString)
    If (dIndex > -1) Then
        Dim lIndex As Integer
        Dim sDomain As String
        sDomain = html.Substring(dIndex + 26, 20)
        lIndex = sDomain.IndexOf("<")
        LblSubDomain.Text = sDomain.Substring(0, lIndex)
    Else
        LblSubDomain.Text = "Cannot Find Sub Domain Extension"
    End If
    
  2. 短代码

    If (WebBrowser.DocumentText.IndexOf("size=" & Chr(34) & "15" & Chr(34) & " maxlength=" & Chr(34) & "40" & Chr(34) & ">") > -1) Then
        LblSubDomain.Text = WebBrowser.DocumentText.Substring(WebBrowser.DocumentText.IndexOf("size=" & Chr(34) & "15" & Chr(34) & " maxlength=" & Chr(34) & "40" & Chr(34) & ">") + 26, 20).Substring(0, WebBrowser.DocumentText.Substring(WebBrowser.DocumentText.IndexOf("size=" & Chr(34) & "15" & Chr(34) & " maxlength=" & Chr(34) & "40" & Chr(34) & ">") + 26, 20).IndexOf("<"))
    Else
        LblSubDomain.Text = "Cannot Find Sub Domain Extension"
    End If
    

我的问题是:这两个代码块中的哪一个对性能的影响最小,或者 VB2012 是否将其编译为一行代码以使其没有区别?

非常感谢你们,我希望我的问题在 Stack Overflow 的预期范围内

4

1 回答 1

3

他们将在性能方面有效地生成相同的代码。

从可维护性和调试的角度来看,选项 #1 更受欢迎,因为它允许通过 Visual Studio 更轻松地注入断点,并且通常更容易理解,因为每行中的逻辑量更小。

我实际上主张在这两个选项之间建立一个快乐的媒介,称之为选项#1.5:

Dim html As String = WebBrowser.DocumentText
Dim htmlString As String = "size=" & Chr(34) & "15" & Chr(34) & " maxlength=" & Chr(34) & "40" & Chr(34) & ">"
Dim dIndex As Integer = html.IndexOf(htmlString)

If (dIndex > -1) Then
    Dim lIndex As Integer = sDomain.IndexOf("<")
    Dim sDomain As String = html.Substring(dIndex + 26, 20)

    LblSubDomain.Text = sDomain.Substring(0, lIndex)
Else
    LblSubDomain.Text = "Cannot Find Sub Domain Extension"
End If

这样可以减少总代码行数,但保留了选项 #1 提供的大部分可读性、可维护性和可调试性。

下面是通过 Reflector 反编译成中间语言 (IL) 的代码:

注意:MySub()选项#1 是否被反编译为 IL。

.method public instance void MySub() cil managed
{
    .maxstack 4
    .locals init (
        [0] int32 num,
        [1] string str,
        [2] string str2,
        [3] int32 num2,
        [4] string str3,
        [5] bool flag)
    L_0000: nop 
    L_0001: ldarg.0 
    L_0002: callvirt instance class    [System.Windows.Forms]System.Windows.Forms.WebBrowser WindowsApplication3.Form1::get_WebBrowser()
    L_0007: callvirt instance string [System.Windows.Forms]System.Windows.Forms.WebBrowser::get_DocumentText()
    L_000c: stloc.1 
    L_000d: ldstr "size=\"15\" maxlength=\"40\">"
    L_0012: stloc.2 
    L_0013: ldloc.1 
    L_0014: ldloc.2 
    L_0015: callvirt instance int32 [mscorlib]System.String::IndexOf(string)
    L_001a: stloc.0 
    L_001b: ldloc.0 
    L_001c: ldc.i4.m1 
    L_001d: cgt 
    L_001f: stloc.s flag
    L_0021: ldloc.s flag
    L_0023: brfalse.s L_0057
    L_0025: ldloc.1 
    L_0026: ldloc.0 
    L_0027: ldc.i4.s 0x1a
    L_0029: add.ovf 
    L_002a: ldc.i4.s 20
    L_002c: callvirt instance string [mscorlib]System.String::Substring(int32, int32)
    L_0031: stloc.s str3
    L_0033: ldloc.s str3
    L_0035: ldstr "<"
    L_003a: callvirt instance int32 [mscorlib]System.String::IndexOf(string)
    L_003f: stloc.3 
    L_0040: ldarg.0 
    L_0041: callvirt instance class [System.Windows.Forms]System.Windows.Forms.Label WindowsApplication3.Form1::get_LblSubDomain()
    L_0046: ldloc.s str3
    L_0048: ldc.i4.0 
    L_0049: ldloc.3 
    L_004a: callvirt instance string [mscorlib]System.String::Substring(int32, int32)
    L_004f: callvirt instance void [System.Windows.Forms]System.Windows.Forms.Label::set_Text(string)
    L_0054: nop 
    L_0055: br.s L_0069
    L_0057: nop 
    L_0058: ldarg.0 
    L_0059: callvirt instance class [System.Windows.Forms]System.Windows.Forms.Label WindowsApplication3.Form1::get_LblSubDomain()
    L_005e: ldstr "Cannot Find Sub Domain Extension"
    L_0063: callvirt instance void [System.Windows.Forms]System.Windows.Forms.Label::set_Text(string)
    L_0068: nop 
    L_0069: nop 
    L_006a: nop 
    L_006b: ret 
}

注意:MySub2()选项#2 是否被反编译为 IL。

.method public instance void MySub2() cil managed
{
    .maxstack 6
    .locals init (
        [0] bool flag)
    L_0000: nop 
    L_0001: ldarg.0 
    L_0002: callvirt instance class [System.Windows.Forms]System.Windows.Forms.WebBrowser WindowsApplication3.Form1::get_WebBrowser()
    L_0007: callvirt instance string [System.Windows.Forms]System.Windows.Forms.WebBrowser::get_DocumentText()
    L_000c: ldstr "size=\"15\" maxlength=\"40\">"
    L_0011: callvirt instance int32 [mscorlib]System.String::IndexOf(string)
    L_0016: ldc.i4.m1 
    L_0017: cgt 
    L_0019: stloc.0 
    L_001a: ldloc.0 
    L_001b: brfalse.s L_008f
    L_001d: ldarg.0 
    L_001e: callvirt instance class [System.Windows.Forms]System.Windows.Forms.Label WindowsApplication3.Form1::get_LblSubDomain()
    L_0023: ldarg.0 
    L_0024: callvirt instance class [System.Windows.Forms]System.Windows.Forms.WebBrowser WindowsApplication3.Form1::get_WebBrowser()
    L_0029: callvirt instance string [System.Windows.Forms]System.Windows.Forms.WebBrowser::get_DocumentText()
    L_002e: ldarg.0 
    L_002f: callvirt instance class [System.Windows.Forms]System.Windows.Forms.WebBrowser WindowsApplication3.Form1::get_WebBrowser()
    L_0034: callvirt instance string [System.Windows.Forms]System.Windows.Forms.WebBrowser::get_DocumentText()
    L_0039: ldstr "size=\"15\" maxlength=\"40\">"
    L_003e: callvirt instance int32 [mscorlib]System.String::IndexOf(string)
    L_0043: ldc.i4.s 0x1a
    L_0045: add.ovf 
    L_0046: ldc.i4.s 20
    L_0048: callvirt instance string [mscorlib]System.String::Substring(int32, int32)
    L_004d: ldc.i4.0 
    L_004e: ldarg.0 
    L_004f: callvirt instance class [System.Windows.Forms]System.Windows.Forms.WebBrowser WindowsApplication3.Form1::get_WebBrowser()
    L_0054: callvirt instance string [System.Windows.Forms]System.Windows.Forms.WebBrowser::get_DocumentText()
    L_0059: ldarg.0 
    L_005a: callvirt instance class [System.Windows.Forms]System.Windows.Forms.WebBrowser WindowsApplication3.Form1::get_WebBrowser()
    L_005f: callvirt instance string [System.Windows.Forms]System.Windows.Forms.WebBrowser::get_DocumentText()
    L_0064: ldstr "size=\"15\" maxlength=\"40\">"
    L_0069: callvirt instance int32 [mscorlib]System.String::IndexOf(string)
    L_006e: ldc.i4.s 0x1a
    L_0070: add.ovf 
    L_0071: ldc.i4.s 20
    L_0073: callvirt instance string [mscorlib]System.String::Substring(int32, int32)
    L_0078: ldstr "<"
    L_007d: callvirt instance int32 [mscorlib]System.String::IndexOf(string)
    L_0082: callvirt instance string [mscorlib]System.String::Substring(int32, int32)
    L_0087: callvirt instance void [System.Windows.Forms]System.Windows.Forms.Label::set_Text(string)
    L_008c: nop 
    L_008d: br.s L_00a1
    L_008f: nop 
    L_0090: ldarg.0 
    L_0091: callvirt instance class [System.Windows.Forms]System.Windows.Forms.Label WindowsApplication3.Form1::get_LblSubDomain()
    L_0096: ldstr "Cannot Find Sub Domain Extension"
    L_009b: callvirt instance void [System.Windows.Forms]System.Windows.Forms.Label::set_Text(string)
    L_00a0: nop 
    L_00a1: nop 
    L_00a2: nop 
    L_00a3: ret 
}

注意:Reflector在试用期后不再是免费产品,但是有免费的替代品可以为您的代码获取 IL(ildasm是 .NET Framework 中内置的工具,ILSpy是对 Reflector 不再免费的开源响应)

于 2013-09-10T22:41:52.463 回答