vb.net – 哪个更快 – 使用块或Try / Catch / Finally

这是 this的后续问题

我应该坚持使用Try / Catch / Finally构造,还是使用Using构造?

Try / Catch / Finally的示例代码

Dim oRequest As WebRequest
Dim oResponse As HttpWebResponse = nothing
Dim dataStream As Stream = nothing
Dim reader As StreamReader = nothing
Dim responseFromServer As String

Try
        sNewCustomerURL = NewCustomerQueryStringPrepare()

    'make the call to the webservice to add a new customer
    oRequest = WebRequest.Create(sNewCustomerURL)

    oRequest = CType(oRequesC,HttpWebRequest)
    oRequest.Method = "GET"
    oResponse = CType(oRequest.GetResponse(),HttpWebResponse)

    dataStream = oResponse.GetResponseStream()
    reader = New StreamReader(dataStream)
    responseFromServer = reader.ReadToEnd()

        Dim xml As New XmlDocument()
    xml.LoadXml(responseFromServer)
    Dim node As XmlNodeList = xml.GetElementsByTagName("SUCCESS")
    Dim value = CBool(node(0).InnerText)

    'do stuff               


Catch ex As Exception

       'process exception

Finally

    'do cleanup
    oRequest = nothing
    If Not oResponse Is nothing Then
        oResponse.Close()
    End If
    oResponse = nothing
    If Not reader Is nothing Then
        reader.Close()
    End If
    reader = nothing
    If Not dataStream Is nothing Then
        dataStream.Flush()
        dataStream.Close()
    End If
    dataStream = nothing
End Try

我知道Using构造需要什么代码.我只是想知道使用Using构造是否会比较时钟周期更快.

不会有性能差异. using由编译器扩展为try / finally块.

您将看到以下两种方法编译为相同的IL.

void SampleWithUsing()
{
    using (MemoryStream s = new MemoryStream())
    {
        s.WriteByte(1);
    }
}

void SampleWithTryFinally()
{
    MemoryStream s = new MemoryStream();
    try
    {
        s.WriteByte(1);
    }
    finally
    {
        if (s != null) s.dispose();
    }
}

在第一种情况下生成的IL是:

.method private hidebysig instance void  SampleWithUsing() cil managed
{
  // Code size       26 (0x1a)
  .maxstack  2
  .locals init ([0] class [mscorlib]System.IO.MemoryStream s)
  IL_0000:  newobj     instance void [mscorlib]System.IO.MemoryStream::.ctor()
  IL_0005:  stloc.0
  .try
  {
    IL_0006:  ldloc.0
    IL_0007:  ldc.i4.1
    IL_0008:  callvirt   instance void [mscorlib]System.IO.Stream::WriteByte(uint8)
    IL_000d:  leave.s    IL_0019
  }  // end .try
  finally
  {
    IL_000f:  ldloc.0
    IL_0010:  brfalse.s  IL_0018
    IL_0012:  ldloc.0
    IL_0013:  callvirt   instance void [mscorlib]System.Idisposable::dispose()
    IL_0018:  endfinally
  }  // end handler
  IL_0019:  ret
} // end of method Program::SampleWithUsing

在第二种情况下,在C#中使用try / finally,我们得到:

.method private hidebysig instance void  SampleWithTryFinally() cil managed
{
  // Code size       26 (0x1a)
  .maxstack  2
  .locals init ([0] class [mscorlib]System.IO.MemoryStream s)
  IL_0000:  newobj     instance void [mscorlib]System.IO.MemoryStream::.ctor()
  IL_0005:  stloc.0
  .try
  {
    IL_0006:  ldloc.0
    IL_0007:  ldc.i4.1
    IL_0008:  callvirt   instance void [mscorlib]System.IO.Stream::WriteByte(uint8)
    IL_000d:  leave.s    IL_0019
  }  // end .try
  finally
  {
    IL_000f:  ldloc.0
    IL_0010:  brfalse.s  IL_0018
    IL_0012:  ldloc.0
    IL_0013:  callvirt   instance void [mscorlib]System.IO.Stream::dispose()
    IL_0018:  endfinally
  }  // end handler
  IL_0019:  ret
} // end of method Program::SampleWithTryFinally

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...