哪个更快 - 使用块或 Try/Catch/Finally
后续问题
这是这个的 我坚持使用 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 结构是否会比时钟周期更快。
This is a followup question to this
Should I stick with the Try/Catch/Finally construct, or go with the Using construct?
Sample Code for 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
I know what the code would need to be for the Using construct. I just want to know if using the Using construct would be faster comparing clock cycles.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不会有性能差异。
using
被编译器扩展为 try/finally 块。您将看到以下两种方法编译为相同的 IL。
第一种情况生成的 IL 是:
在第二种情况下,使用 C# 中的 try/finally 我们得到:
There won't be a performance difference.
using
is expanded by the compiler to a try/finally block.You will see that the following two methods compile to identical IL.
The IL generated in the first case is:
In the second case with a try/finally in C# we get:
using
编译为try
/catch
/finally
。我真的看不出有任何语义或性能差异,只要您在手动try
/catch
的情况下正确处置对象即可。无论如何,请使用
using
,因为它会自动为您进行清理(实际上在finally
子句中)。即使存在性能差异,也可能很小,因此您有更好的地方可以优化。using
compiles intotry
/catch
/finally
. I really can't see there being any semantical or performance difference, as long as you properly dispose your object in the case of a manualtry
/catch
.In any case, go with
using
, seeing as it does cleanups automagically (actually in thefinally
clause) for you. Even if there is a performance difference, it is likely so minimal that you have better places to optimize.我必须相信,相对于您在这些构造中运行的代码,使用与 try/catch 相比不会对性能产生任何差异。
I have to believe that using versus try/catch is not going to make any difference performance wise relative to the code you are running inside those constructs.
Try/Catch/Finally 更快。
1) 6.638 秒:使用标签
2) 6.265 秒:try/catch/finally
我运行了大约十几次。 Try/Catch/Finally 总是名列前茅。
Try/Catch/Finally is faster.
1) 6.638 sec : using tag
2) 6.265 sec : try/catch/finally
I ran this about a dozen times. Try/Catch/Finally always came out on top.