开发者

performance hit for int + "" instead of using int.ToString()

开发者 https://www.devze.com 2023-01-14 00:11 出处:网络
How much performance hit I will inure if I use following: int i=5; label1.Text = i + \"\"; instead of: int i=5;

How much performance hit I will inure if I use following:

int i=5;
label1.Text = i + "";

instead of:

int i=5;
label1.Tex开发者_运维百科t = i.ToString();


The first method creates an extra object in the process, as it creates an object to box the integer in. The compiler generates this code:

label1.Text = String.Concat((object)i, (object)String.Empty);

The Concat method will call the ToString method on both parameters to get their string values, which effectively unboxes the integer before turning it into a string. It would create another extra object by concatenating the strings, but it has code to catch the special case where one of the strings are empty, so it just returns the string value of the integer.

The second method ends up doing exactly what it says.

You should avoid the first method, not primarily because it performs worse, but because it hides what you really want to do.

The performance difference will be minimal as long as you don't do this in a tight loop (I measured a speed diffence of 5-10%). The extra object that is created is short lived, so it will most likely be collected in the next garbage collection. However, there is no reason to use the first method as the second method exists and does exactly what you want, instead of causing it as a hidden side effect.

Edit:

Note also that the first method should be written i + String.Empty rather than i + "", so it's not even less to type than the second method.


While there may be a difference in performance between the two, ask yourself this question: will this be significant for the performance of the application?

My guess is that it will not be significant.

Measure it and fix it if it is.

Architecture will usually affect performance. Minor language optimization may affect performance, but in general your time is better spend attending to bottlenecks identified through actual measurement.


I haven't profiled it, but logically the first is implicitly converting i to a string, creating a new empty string, and then concatenating the two. The second is merely creating a string representation (one of the three steps). (Of course, the compiler could optimize the first so that they're equivalent).

Except in a very performance-critical part of the app this difference is negligible. More important is the fact that the second is semantically precise -- it says exactly what you're trying to do. The first is potentially confusing. I mean, that's a common enough construct that anyone who reads it will figure it out, but, given the choice, why not pick the more readable code?


Simply write a Console Application that performs one and the other in separate loops for an easily modifiable amount of times and measure how long it takes to find out?


To prove Guffas answer, heres the generated IL-Code:

//000015:             String s1 = number.ToString();
  IL_0004:  ldloca.s   number
  IL_0006:  call       instance string [mscorlib]System.Int32::ToString()
  IL_000b:  stloc.1
//000016:             String s2 = number + "";
  IL_000c:  ldloc.0
  IL_000d:  box        [mscorlib]System.Int32
  IL_0012:  call       string [mscorlib]System.String::Concat(object)
  IL_0017:  stloc.2


Negligable. Don't bother to pay it attention.


I'm thinking that the benefits (if there is one) will be negligible in favor of code readability in this particular case. The second case is obvious and quick to the reader's eye; the first one normally requires a double take.

For an operation this trivial, I personally think that readability wins out.


I will use label1.Text = i.ToString(); even though it has a worse performance(in fact I think it has a better performance than i+""). Here are some reasons:

  • i+"" seems to be a hard code, which brings very bad smell. If you really want to use + operator, i+string.Empty is better(but still not a good idea).
  • i.ToString() is easier to read, which means "oh, i need the string of i, to be placed in some label". It presents your willing. While i+"" means "put the result of adding i and an empty string to some label".


On my computer, i.ToString() is about 20 nanoseconds (2e-8) faster than i + "". In other words, there is no difference. You would have to do this operation tens of millions of times before it even made 1 second difference!

0

精彩评论

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