|
6
|
| crackedcornjimmy · 技术社区 · 16 年前 |
|
|
1
9
不会有性能差异。
您将看到以下两个方法编译为相同的IL。
第一种情况下生成的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中尝试/最终,我们得到: .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
|
|
|
2
2
不管怎样,继续
|
|
|
3
0
我必须相信,相对于您在这些构造中运行的代码,使用和Try/Catch不会对性能产生任何影响。 |
|
|
4
0
Try/Catch/Finally更快。
1)6.638秒:使用标签
我跑了十几次。尝试/抓住/最后总是出类拔萃。
|
|
|
Mear1628 · vb.net从随机选择的端口号接收UDP 1 年前 |
|
|
user1946932 · .Net正则表达式在所有字符前添加空格 1 年前 |
|
|
user1946932 · 正则表达式:删除连续两个以上的<br>? 2 年前 |
|
|
Josh · 通过反思访问孩子的财产 2 年前 |
|
|
Khu RJ · 更新网格中的单元格-DevExpress网格控件 2 年前 |
|
|
Mick · VB.NET Regex。替换不起作用(数组未更新) 2 年前 |