开发者

which among these is better with respect to performance?

开发者 https://www.devze.com 2022-12-17 05:54 出处:网络
what is the difference between these two statements? which is better开发者_StackOverflow中文版 with respect to performance?

what is the difference between these two statements? which is better开发者_StackOverflow中文版 with respect to performance?

Console.Writeline(i);
Console.Writeline(i.toString());

where i is a string or an integer.


The bottom line is that writing to the console is bound to dominate the performance here - even if you're redirecting it to some sort of "null" sink.

The difference, IMO, is that

Console.WriteLine(i);

is simpler to read... so that's what I'd use until I'd proven that using the slightly-less-readable form gave a concrete benefit. In this case, neither form would end up boxing i when it's an integer, because there's an overload for WriteLine(int). A slightly more interesting question is between these two lines:

Console.WriteLine("Some format {0} stuff", i);
Console.WriteLine("Some format {0} stuff", i.ToString());

The first form will box the integer; the second won't. The difference in performance? Nothing significant.


There is no practical difference, both will call ToString to convert the number to a string.


There should be no difference. The Console.WriteLine method is supposed to automaticaly calls the ToString() method if you don't explicitly do so yourself. However, if you Look at the IL code, I was surprised to see that they are not identical. Obviously, the Console.Writeline(int) must internally convert the int to a string of decimal characters, but this is not obvious from the IL...

For this code:

static void Main(string[] args)
{
    int i = 34;
    Console.WriteLine(i);
    Console.WriteLine(i.ToString());

}

The IL is

.method private hidebysig static void Main(string[] args) cil managed
{
    .entrypoint
    .maxstack 1
    .locals init (
        [0] int32 i)
    L_0000: nop 
    L_0001: ldc.i4.s 0x22
    L_0003: stloc.0 
    L_0004: ldloc.0 
    L_0005: call void [mscorlib]System.Console::WriteLine(int32)
    L_000a: nop 
    L_000b: ldloca.s i
    L_000d: call instance string [mscorlib]System.Int32::ToString()
    L_0012: call void [mscorlib]System.Console::WriteLine(string)
    L_0017: nop 
    L_0018: ret 
}

Using Jon's example, (with string.format), The first case (w/o the ToString()) the value is boxed... Code:

    int i = 34;
    Console.WriteLine(string.Format("Some format {0} stuff", i));
    Console.WriteLine(string.Format("Some format {0} stuff", i.ToString()));

the IL is

.method private hidebysig static void Main(string[] args) cil managed
{
    .entrypoint
    .maxstack 2
    .locals init (
        [0] int32 i)
    L_0000: nop 
    L_0001: ldc.i4.s 0x22
    L_0003: stloc.0 
    L_0004: ldstr "Some format {0} stuff"
    L_0009: ldloc.0 
    L_000a: box int32
    L_000f: call string [mscorlib]System.String::Format(string, object)
    L_0014: call void [mscorlib]System.Console::WriteLine(string)
    L_0019: nop 
    L_001a: ldstr "Some format {0} stuff"
    L_001f: ldloca.s i
    L_0021: call instance string [mscorlib]System.Int32::ToString()
    L_0026: call string [mscorlib]System.String::Format(string, object)
    L_002b: call void [mscorlib]System.Console::WriteLine(string)
    L_0030: nop 
    L_0031: ret 
}


First off, optimizing to this level is really not going to get you any benefits. Optimize your algorithms, then profile, and optimize your bottlenecks. I'd focus on which is more readable and maintainable (in which I prefer the first, in both cases...)

However, from a performance standpoint, the first will typically be better. If you call the first with an int, it effectively calls the second. With a string, you get an extra (no-op) method call.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号