class Program {static void Main (string [] args) {// print One Word string nmls str1 = "One"; str1 + = "Word"; Console.WriteLine (str1); string str2 = "One" + "Word"; Console.WriteLine nmls (str2); // print One Word43 int i = 43; Console.WriteLine (str1 + i); Console.WriteLine nmls (str1 + i.ToString ());}} Second, the above C # generated CIL code as follows:
.method private nmls hidebysig static void Main (string [] args) cil managed {// initialization code size .entrypoint // 80 (0x50) .maxstack 2 .locals init ([0] string str1, [1] string str2, [2 ] int32 i) // adding two strings nmls in two steps IL_0000: nop IL_0001: ldstr "One" IL_0006: stloc 0 IL_0007:. ldloc 0 IL_0008:. ldstr "Word" IL_000d: call string [mscorlib] System .String :: Concat (string, string) nmls IL_0012: stloc 0 IL_0013:. ldloc 0 IL_0014:. call void [mscorlib] System.Console :: WriteLine (string) // adding two strings are in the same line inside nmls When translated into CIL had joined together IL_0019: nop IL_001a: ldstr "One Word" IL_001f: stloc 1 IL_0020:. ldloc 1 IL_0021:. call void [mscorlib] System.Console :: WriteLine (string) nmls // will Int32 character packing again and String merge print IL_0026: nop IL_0027: ldc.i4.s 43 IL_0029: stloc 2 IL_002a:. ldloc 0 IL_002b:.. ldloc 2 IL_002c: box [mscorlib] System.Int32 nmls IL_0031: call string [mscorlib] System .String :: Concat (object, object) IL_0036: call void [mscorlib] System.Console :: WriteLine (string) // direct call numbers ToString () method IL_003b: nop IL_003c: ldloc 0 IL_003d:. ldloca.si IL_003f nmls : call instance string [mscorlib] System.Int32 :: ToString () IL_0044: call string [mscorlib] System.String :: Concat (string, string) IL_0049: call void [mscorlib] System.Console :: WriteLine (string) IL_004e : nop IL_004f: ret} // end of method Program :: Main
// The first two strings in two steps by adding IL_0000: nop IL_0001: ldstr "One" // One string will keep its references to the heap and push IL_0006:. Stloc 0 // from One string top of the stack reference address pops up to the first parameter 0 IL_0007:. ldloc 0 // Load the local variable index 0 onto the evaluation stack IL_0008: ldstr "Word" // will save Word string to the heap and to return a reference to a computer nmls system function // call stack on top of the two strings are added to the top of the stack the result stored IL_000d: call string [mscorlib] System.String :: Concat (string, string) IL_0012: stloc. 0 // address pop references to the first parameter 0 IL_0013 One string from the top of the stack: ldloc 0 // Load the local variable index 0 onto the evaluation stack // call the system parameters Print One Word IL_0014:. call void [mscorlib] System.Console :: WriteLine (string)
// Two strings on the same line which proceeds the sum is translated into CIL had joined together IL_0019: nop IL_001a: ldstr "One Word" // direct has been configured to One Word IL_001f:. Stloc 1 // from the stack One Word string reference address pops up to the top of the first parameter 1 IL_0020: ldloc 1 // Load the local variable index 1 onto the evaluation stack // call the system parameters Print One Word IL_0021:. call void [mscorlib] System .Console :: WriteLine (string)
// The character packing again Int32 and String merge print IL_0026: nop IL_0027: ldc.i4.s 43 IL_0029: stloc 2 IL_002a:. Ldloc 0 IL_002b:. Ldloc 2 IL_002c:. Box [mscorlib] System.Int32 // in There is a boxing operation IL_0031: call string [mscorlib] System.String :: Concat (object, object) IL_0036: call void [mscorlib] System.Console :: WriteLine (string)
// Direct call numbers ToString () method IL_003b: nop IL_003c: ldloc 0 IL_003d:. Ldloca.si // There is no packing operation, just call overloaded ToString () method IL_003f: call instance nmls string [mscorlib] nmls System.Int32 :: ToString () IL_0044: call string [mscorlib] System.String :: Concat (string, string) IL_0049: call void [mscorlib] System.Console :: WriteLine (string) IL_004e: nop Conclusion: Here we The first analysis is carried out on the Int32 value box packing operation, and then call the system function and adding a string before they get results. The second case is a direct function of the Int32 call its overloaded ToString () to get the character again Int32 value added to the first character. Here we avoid the boxing heap allocation address lookup spare stack address other problems, so if a loop again, the second situation is certainly much higher efficiency.
No comments:
Post a Comment