try catch performance

前端 未结 7 955
小蘑菇
小蘑菇 2020-11-29 09:28

This article on MSDN states that you can use as many try catch blocks as you want and not incur any performance cost as long no actual exception is thrown.
Since I alway

7条回答
  •  迷失自我
    2020-11-29 09:53

    Note that I only have Mono available:

    // a.cs
    public class x {
        static void Main() {
            int x = 0;
            x += 5;
            return ;
        }
    }
    
    
    // b.cs
    public class x {
        static void Main() {
            int x = 0;
            try {
                x += 5;
            } catch (System.Exception) {
                throw;
            }
            return ;
        }
    }
    

    Disassembling these:

    // a.cs
           default void Main ()  cil managed
    {
        // Method begins at RVA 0x20f4
        .entrypoint
        // Code size 7 (0x7)
        .maxstack 3
        .locals init (
                int32   V_0)
        IL_0000:  ldc.i4.0
        IL_0001:  stloc.0
        IL_0002:  ldloc.0
        IL_0003:  ldc.i4.5
        IL_0004:  add
        IL_0005:  stloc.0
        IL_0006:  ret
    } // end of method x::Main
    

    and

    // b.cs
          default void Main ()  cil managed
    {
        // Method begins at RVA 0x20f4
        .entrypoint
        // Code size 20 (0x14)
        .maxstack 3
        .locals init (
                int32   V_0)
        IL_0000:  ldc.i4.0
        IL_0001:  stloc.0
        .try { // 0
          IL_0002:  ldloc.0
          IL_0003:  ldc.i4.5
          IL_0004:  add
          IL_0005:  stloc.0
          IL_0006:  leave IL_0013
    
        } // end .try 0
        catch class [mscorlib]System.Exception { // 0
          IL_000b:  pop
          IL_000c:  rethrow
          IL_000e:  leave IL_0013
    
        } // end handler 0
        IL_0013:  ret
    } // end of method x::Main
    

    The main difference I see is a.cs goes straight to ret at IL_0006, whereas b.cs has to leave IL_0013 at IL_006. My best guess with my example, is that the leave is a (relatively) expensive jump when compiled to machine code -- that may or may not be the case, especially in your for loop. That is to say, the try-catch has no inherent overhead, but jumping over the catch has a cost, like any conditional branch.

提交回复
热议问题