大家都知道,异常是有性能损失的。但是似乎很多人并不理解异常损失的性能在哪里。
我听见很多声音说,使用try-catch会牺牲性能。希望大家看下下面的代码
static int Test1(int a, int b)
{
try
{
if (a > b)
return a;
return b;
}
catch
{
return -1;
}
}
static int Test2(int a, int b)
{
if (a > b)
return a;
return b;
}
{
try
{
if (a > b)
return a;
return b;
}
catch
{
return -1;
}
}
static int Test2(int a, int b)
{
if (a > b)
return a;
return b;
}
使用ILDasm工具查看,IL代码分别如下
.method private hidebysig static int32 Test1(int32 a,
int32 b) cil managed
{
// 代码大小 30 (0x1e)
.maxstack 2
.locals init ([0] int32 CS$1$0000,
[1] bool CS$4$0001)
IL_0000: nop
.try
{
IL_0001: nop
IL_0002: ldarg.0
IL_0003: ldarg.1
IL_0004: cgt
IL_0006: ldc.i4.0
IL_0007: ceq
IL_0009: stloc.1
IL_000a: ldloc.1
IL_000b: brtrue.s IL_0011
IL_000d: ldarg.0
IL_000e: stloc.0
IL_000f: leave.s IL_001b
IL_0011: ldarg.1
IL_0012: stloc.0
IL_0013: leave.s IL_001b
} // end .try
catch [mscorlib]System.Object
{
IL_0015: pop
IL_0016: nop
IL_0017: ldc.i4.m1
IL_0018: stloc.0
IL_0019: leave.s IL_001b
} // end handler
IL_001b: nop
IL_001c: ldloc.0
IL_001d: ret
} // end of method Program::Test1
int32 b) cil managed
{
// 代码大小 30 (0x1e)
.maxstack 2
.locals init ([0] int32 CS$1$0000,
[1] bool CS$4$0001)
IL_0000: nop
.try
{
IL_0001: nop
IL_0002: ldarg.0
IL_0003: ldarg.1
IL_0004: cgt
IL_0006: ldc.i4.0
IL_0007: ceq
IL_0009: stloc.1
IL_000a: ldloc.1
IL_000b: brtrue.s IL_0011
IL_000d: ldarg.0
IL_000e: stloc.0
IL_000f: leave.s IL_001b
IL_0011: ldarg.1
IL_0012: stloc.0
IL_0013: leave.s IL_001b
} // end .try
catch [mscorlib]System.Object
{
IL_0015: pop
IL_0016: nop
IL_0017: ldc.i4.m1
IL_0018: stloc.0
IL_0019: leave.s IL_001b
} // end handler
IL_001b: nop
IL_001c: ldloc.0
IL_001d: ret
} // end of method Program::Test1
.method private hidebysig static int32 Test2(int32 a,
int32 b) cil managed
{
// 代码大小 22 (0x16)
.maxstack 2
.locals init ([0] int32 CS$1$0000,
[1] bool CS$4$0001)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldarg.1
IL_0003: cgt
IL_0005: ldc.i4.0
IL_0006: ceq
IL_0008: stloc.1
IL_0009: ldloc.1
IL_000a: brtrue.s IL_0010
IL_000c: ldarg.0
IL_000d: stloc.0
IL_000e: br.s IL_0014
IL_0010: ldarg.1
IL_0011: stloc.0
IL_0012: br.s IL_0014
IL_0014: ldloc.0
IL_0015: ret
} // end of method Program::Test2
int32 b) cil managed
{
// 代码大小 22 (0x16)
.maxstack 2
.locals init ([0] int32 CS$1$0000,
[1] bool CS$4$0001)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldarg.1
IL_0003: cgt
IL_0005: ldc.i4.0
IL_0006: ceq
IL_0008: stloc.1
IL_0009: ldloc.1
IL_000a: brtrue.s IL_0010
IL_000c: ldarg.0
IL_000d: stloc.0
IL_000e: br.s IL_0014
IL_0010: ldarg.1
IL_0011: stloc.0
IL_0012: br.s IL_0014
IL_0014: ldloc.0
IL_0015: ret
} // end of method Program::Test2
此处我们只关心try区块,即未发生异常的时候,对于Test1来讲,IL代码多出了8个字节来保存catch的处理代码,这一点对性能和资源几乎是微不足道的。
我们看到当Test1执行到IL_000f或者IL_0013的时候,将数据出栈并使用leave.s退出try区块转向IL_001b地址,然后将数据入栈并返回。
对于Test2来讲,执行到IL_000e或者IL_0012的时候, 直接退出,并将数据入栈然后返回。
实际测试中,二者的在不出错的前提下,性能差距已经到了百分之0.00几的程度,基本不需要考虑在内了。
那么回到正题,那么性能的损失点在于哪里呢?
不知道大家有没有看过自己写的程序抛出异常后的一些详细信息,.NET会在发生异常的时候,创建异常对象当创建一个异常时,需要收集一个栈跟踪,这个栈跟踪用于描述异常是在何处创建的。这才是性能的损失点。
所以我们要做的,就是尽可能的避免抛出异常,当然避免不是代表着把异常吞了。
try{}
catch{return;}
的做法千万不可采用,这对你的程序安全和稳定会埋下重大隐患。
为了提高这方面的性能,才引入了Test-Doer模式与Try-Parse模式