我对 VB.NET 比较陌生,并且是自学编程,所以我没有很多基础知识和概念,所以请原谅我的天真和无知。
我很好奇逐步或仅在一长串代码中编写 VB 函数的逻辑、性能和效率。
下面是我的一个程序中的一个小函数,用于解析一些 HTML。请注意,代码只是一个随机示例,两个代码块执行完全相同的功能。它们在这里是为了说明我的观点,分别将冗长的、过程性声明的、分段的等代码与一段冗长的简洁代码进行对比。
长代码
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
短代码
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 的预期范围内
最佳答案
在性能方面,它们将有效地生成相同的代码。
从可维护性和调试的角度来看,选项 #1 更受欢迎,因为它允许通过 Visual Studio 更轻松地注入(inject)断点,并且通常更容易理解,因为每行中的逻辑量较小。
我实际上提倡在这两个选项之间选择一个中间值,称之为选项 #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()
是 Option #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()
是 Option #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 不再免费的回应)
关于vb.net - 在单个语句中编写代码然后逐步分解它是否有更多的开销?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18729785/