2015-01-04 63 views
1

我不知道正確的方式來搜索這個問題,但這是我做了很多事情,並想弄清楚,如果我一直默默地潛水我的代碼。是這樣的:繞過變量的影響

Int32 x = 5; 
Int32 y = 7; 
Int32 z = x+y; 

Console.Write(z.ToString()); 

與此相同:

Int32 x = 5; 
Int32 y = 7; 

Console.Write((x+y).ToString()); 

我傾向於寫的第一個比第二多。它更冗長,但我發現調試起來更簡單很多,因爲我可以看到方法的結果。

  • 對這些有什麼區別,性能或JIT編譯器明智嗎?
  • 是否使用值或引用類型有關係嗎?

我正在構建和部署.net 4.5應用程序,64位。

+4

,直到你有_real_性能問題不要擔心(可能)浪費納秒。儘量編寫清晰且可維護的代碼。 – HABO

+0

您不指定您正在調查此問題的編譯器,運行時或目標平臺。 –

+0

不要試圖「幫助」編譯器。任何半面體優化JIT都會將你的小片段縮減爲相當於Console.Write(「12」)。將CIL代碼轉換爲JIT的中間表示形式時,最有可能丟棄表達式是否存儲在變量中的信息。 –

回答

3

讓他們看,易圖,一出


,你可以在這兩個,調試/發行版本看到的,給幾乎相同的結果。

的第二種方法,它會創建一個臨時變量,將做完全一樣,你對你的第一個方法


DEBUG方法一(第一個代碼段)

.method private hidebysig static 
    void Method1() cil managed 
{ 
    // Method begins at RVA 0x2060 
    // Code size 23 (0x17) 
    .maxstack 2 
    .locals init (
     [0] int32 x, 
     [1] int32 y, 
     [2] int32 z 
    ) 

    IL_0000: nop 
    IL_0001: ldc.i4.5 
    IL_0002: stloc.0 
    IL_0003: ldc.i4.7 
    IL_0004: stloc.1 
    IL_0005: ldloc.0 
    IL_0006: ldloc.1 
    IL_0007: add 
    IL_0008: stloc.2 
    IL_0009: ldloca.s z 
    IL_000b: call instance string [mscorlib]System.Int32::ToString() 
    IL_0010: call void [mscorlib]System.Console::Write(string) 
    IL_0015: nop 
    IL_0016: ret 
} // end of method Program::Method1 

DEBUG的Int32 z = x+y; Method2(Second code snippet)

.method private hidebysig static 
    void Method2() cil managed 
{ 
    // Method begins at RVA 0x2084 
    // Code size 23 (0x17) 
    .maxstack 2 
    .locals init (
     [0] int32 x, 
     [1] int32 y, 
     [2] int32 CS$0$0000 
    ) 

    IL_0000: nop 
    IL_0001: ldc.i4.5 
    IL_0002: stloc.0 
    IL_0003: ldc.i4.7 
    IL_0004: stloc.1 
    IL_0005: ldloc.0 
    IL_0006: ldloc.1 
    IL_0007: add 
    IL_0008: stloc.2 
    IL_0009: ldloca.s CS$0$0000 
    IL_000b: call instance string [mscorlib]System.Int32::ToString() 
    IL_0010: call void [mscorlib]System.Console::Write(string) 
    IL_0015: nop 
    IL_0016: ret 
} // end of method Program::Method2 

RELEASE Method1(Fir ST代碼段)

.method private hidebysig static 
    void Method1() cil managed 
{ 
    // Method begins at RVA 0x205c 
    // Code size 21 (0x15) 
    .maxstack 2 
    .locals init (
     [0] int32 x, 
     [1] int32 y, 
     [2] int32 z 
    ) 

    IL_0000: ldc.i4.5 
    IL_0001: stloc.0 
    IL_0002: ldc.i4.7 
    IL_0003: stloc.1 
    IL_0004: ldloc.0 
    IL_0005: ldloc.1 
    IL_0006: add 
    IL_0007: stloc.2 
    IL_0008: ldloca.s z 
    IL_000a: call instance string [mscorlib]System.Int32::ToString() 
    IL_000f: call void [mscorlib]System.Console::Write(string) 
    IL_0014: ret 
} // end of method Program::Method1 

RELEASE方法2(第二代碼片段)

.method private hidebysig static 
    void Method2() cil managed 
{ 
    // Method begins at RVA 0x2080 
    // Code size 21 (0x15) 
    .maxstack 2 
    .locals init (
     [0] int32 x, 
     [1] int32 y, 
     [2] int32 CS$0$0000 
    ) 

    IL_0000: ldc.i4.5 
    IL_0001: stloc.0 
    IL_0002: ldc.i4.7 
    IL_0003: stloc.1 
    IL_0004: ldloc.0 
    IL_0005: ldloc.1 
    IL_0006: add 
    IL_0007: stloc.2 
    IL_0008: ldloca.s CS$0$0000 
    IL_000a: call instance string [mscorlib]System.Int32::ToString() 
    IL_000f: call void [mscorlib]System.Console::Write(string) 
    IL_0014: ret 
} // end of method Program::Method2