Simple code performance difference

xiaoxiao2021-03-06  79

".NET Framework Program Design", I'm reintegrating, write a function, which looks more convenient:

public

Void

Boxing ()

{INT32 V = 5; console.writeLine ("{0}, {1}, {2}", V, v, v);

public

Void

Boxing1 ()

{INT32 V = 5; Object O = V; console.writeLine ("{0}, {1}, {2}", o, o);

The output of the two functions is the same, but which function is better?

The first is clearly simple, but in performance, look at the anti-compiletable code:

Boxing function:

.method

public

HideBysig Instance

Void

Boxing () CIL Managed

{// code size: 31 byte (s) .maxstack 4 .locals (int32 v_0) l_0000: ldc.i4.5 l_0001: STLOC.0 L_0002: ldstr "{0}, {1}, {2}" L_0007: ldloc.0 L_0008: box int32 L_000d: ldloc.0 L_000e: box int32 L_0013: ldloc.0 L_0014: box int32 L_0019: call void [mscorlib] System.Console :: WriteLine (string, object, object, object) L_001e: ret }

Boxing1 function:

.method

public

HideBysig Instance

Void

Boxing1 () CIL Managed

{// code size: 23 byte (s) .maxstack 4 .locals (int32 v_0, object v_1) l_0000: ldc.i4.5 l_0001: STLOC.0 L_0002: LDLOC.0 L_0003: Box INT32 L_0008: STLOC.1 L_0009 : Ldstr "{0}, {1}, {2}" l_000e: ldloc.1 l_000f: ldloc.1 l_0010: ldloc.1 l_0011: Call void [mscorlib] system.console :: writeline (String, Object, Object, Object) L_0016: Ret}

The first function has three packing operations, and the second only once, in memory and time consumption.

Such a simple code, there is such a big difference in performance, I don't know how much problems in the program.

转载请注明原文地址:https://www.9cbs.com/read-121956.html

New Post(0)