Performance is something you measure. If it doesn't result in a measurable
performance different (e.g., if the JIT smooths out the difference), there's
no performance issue.
And that's aside from the fact that even if you can measure it, this sort of
micro-optimization would be about 10,000th on the list of things you should
worry about. It's like skipping lunch to improve your gas mileage.
To be honest Mike, I'm really not that interested. This thread has
gone totally off the rails. Concat is a red herring. You insist on
speaking in specifics while I deal with generalities. I posted the IL
to show that the int is boxed and it will then call Concat again. I
prefer using ToString because to my mind it indicates that I want a
string. In reality I'd probably use something more like the format I
used to output the three tick counts. For the sake of the interview
question I'd take the ToString version for the following reason:
Jon, I take your point regarding readability, I always do. In this
case I disagree to a point. As you say, it should always be implied
that if I + a string and an int I want the int's string
representation, my point was to show if you lose the string the +
reverts to a mathematical operator giving the wrong result (I have
seen this occur in code where a developer has changed how a composite
key was generated). Again, this is all very specific and not what I
originally intended to show.
I'd also argue that it's moot anyway. The majority of the time I'm
using a string builder, or not concatentating and having to use
ToString anyway. For that reason, I can also say I use it out of force
of habit.
Brian, in my example I removed all the WriteLine statements for
exactly that reason. I understand your point, I'm not going to have an
application that just Concats strings and then exits. Again, this is
too specific an example and I was responding to rage filled Mike in
his initial IL bashing. If I'm operating on a huge set of data I'll
take any optimisations I can get. If I can knock five minutes off an
hour long job it's worth it (to me, in my environment). I won't get
that from Concat sure, that's not really the point. This isn't a real
world test, it's a poor interview question that does not represent the
programming challenges we all face every day.