String.Format(int) vs. int.ToString()

J

Jiho Han

Which is faster?

int x = 5;

string v = String.Format("{0}", x);

or

string v = x.ToString();

Looking at it, it seems almost obvious that the latter would be faster since
the first would involve creating a new string object and formatting - even
if simple. I just wanted to know if anyone else have comments.

Here's another:

int increment = 0;
string controlname = "abc";

string v = String.Format("{0}{1}", controlname, increment);

or

string v = controlname + increment.ToString();

or

StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0}{1}", controlname, incremenet);
string v = sb.ToString();

I wonder whether String.Format is internally implemented as the last bit of
code snippet.
I'd appreciate any thoughts on these items. I use all of these approaches
everywhere and I often wonder if I am doing the right thing.

Thanks much.
Jiho Han
 
D

Daniel Billingsley

Jiho Han said:
Which is faster?

int x = 5;

string v = String.Format("{0}", x);

or

string v = x.ToString();

Looking at it, it seems almost obvious that the latter would be faster since
the first would involve creating a new string object and formatting - even
if simple. I just wanted to know if anyone else have comments.

They both create strings. I'm quite confident the latter will be faster, I
expect it's because it just does what it does and doesn't have to interpret
your format string.
Here's another:

int increment = 0;
string controlname = "abc";

string v = String.Format("{0}{1}", controlname, increment);

or

string v = controlname + increment.ToString();

or

StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0}{1}", controlname, incremenet);
string v = sb.ToString();

For creating such simple strings simple concatenation (+) is the fastest,
String.Format is the slowest.

But keep in mind that we're talking about a difference of microseconds for
any particular string build. Unless that kind of performance difference is
going to be significant to your application, I personally would favor the
String.Format() code over the alternatives for readability/maintainability.
 
J

Jon Skeet [C# MVP]

Jiho Han said:
Which is faster?

int x = 5;

string v = String.Format("{0}", x);

or

string v = x.ToString();

Looking at it, it seems almost obvious that the latter would be faster since
the first would involve creating a new string object and formatting - even
if simple. I just wanted to know if anyone else have comments.

Well, both require creating a new string object. I would expect the
latter to be slightly faster, but more importantly, it's more readable.
Here's another:

int increment = 0;
string controlname = "abc";

string v = String.Format("{0}{1}", controlname, increment);

or

string v = controlname + increment.ToString();

or

StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0}{1}", controlname, incremenet);
string v = sb.ToString();

The last of these is the least desirable - it's less readable than
either of the others. Depending on the situation, I might use the first
or second form.
I wonder whether String.Format is internally implemented as the last bit of
code snippet.
I'd appreciate any thoughts on these items. I use all of these approaches
everywhere and I often wonder if I am doing the right thing.

I believe it's more likely to be an optimised version of the last
snippet. For instance, it might make an educated guess at the length of
the final string, and so create an appropriate StringBuilder. Or it
might just be the "dumb" version - or something entirely different!

It's unlikely to make very much difference performance-wise - so I'd
stick with the most readable version.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top