468,765 Members | 1,730 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

String.Format vs + operator

Which is better, which is faster, which is easier etc... ?????

String.Format ( "yadda {0} yadda {1}", x, y )

"yadda" + x.ToString() + " yadda" + y.tostring();
My code has a mish mash of both and I am wondewring if I should standardize
or not ??
Thnanks,

JIM
Jun 25 '07 #1
15 2272
On Jun 25, 11:10 am, "James" <nos...@hypercon.netwrote:
Which is better, which is faster, which is easier etc... ?????

String.Format ( "yadda {0} yadda {1}", x, y )

"yadda" + x.ToString() + " yadda" + y.tostring();

My code has a mish mash of both and I am wondewring if I should standardize
or not ??
In addition to what other posters have mentioned, keep in mind that if
you ever want to internationalize your code, the "+" operator way is a
non-starter.

Jun 25 '07 #2
On Mon, 25 Jun 2007 20:18:41 +0200, Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
James,

Using the + operator will be faster, as it will just pre-allocate the
string and copy the various elements together. With Format, the format
string is parsed, and that is going to require some overhead.

However, I do think that you should carefully consider the overhead
versus the readability/maintainability of the code, as I think that the call
to Format is MUCH much better in this arena.

Of course, if you have a good number of these operations being
performed, then you could gain some performance by moving to the concat
operator (+).

In some cases I agree, but in many cases readability can be much improved by not calling ToString(), in which case the OP's example narrows downto

string s = "yadda " + x + " yadda " + y;

which I would argue is more readable (syntax coloring will make it even more readable) than

string s = string.Format("yadda {0} yadda {1}", x, y);
PS! If any of the parts of a string concatenation contains a string literal, ToString() will automatically be called on other parts as well.

--
Happy coding!
Morten Wennevik [C# MVP]
Jun 25 '07 #3
Ok, thanks,

JIM
"Tom Spink" <ts****@gmail.comwrote in message
news:uj**************@TK2MSFTNGP03.phx.gbl...
James wrote:
>Which is better, which is faster, which is easier etc... ?????

String.Format ( "yadda {0} yadda {1}", x, y )

"yadda" + x.ToString() + " yadda" + y.tostring();
My code has a mish mash of both and I am wondewring if I should
standardize or not ??
Thnanks,

JIM

Hi Jim,

IIRC, concatenation is faster, but you should choose based on whether you
actually need to gain speed when concatenating strings (i.e. is your
application performance critical) and then decided whether you should
sacrifice code readability for a slight performance improvement.

--
Tom Spink
University of Edinburgh

Jun 25 '07 #4
Excellent, thanks

JIM
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:ef**************@TK2MSFTNGP04.phx.gbl...
James,

Using the + operator will be faster, as it will just pre-allocate the
string and copy the various elements together. With Format, the format
string is parsed, and that is going to require some overhead.

However, I do think that you should carefully consider the overhead
versus the readability/maintainability of the code, as I think that the
call to Format is MUCH much better in this arena.

Of course, if you have a good number of these operations being
performed, then you could gain some performance by moving to the concat
operator (+).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"James" <no****@hypercon.netwrote in message
news:OY**************@TK2MSFTNGP06.phx.gbl...
>Which is better, which is faster, which is easier etc... ?????

String.Format ( "yadda {0} yadda {1}", x, y )

"yadda" + x.ToString() + " yadda" + y.tostring();
My code has a mish mash of both and I am wondewring if I should
standardize or not ??
Thnanks,

JIM


Jun 25 '07 #5
Readability, versus perfrmance.

The funny thing here is that, for me, the + operator is easier to read than
and brackets {0} etc... So, for me it looks like I get readability and
performance with the + operator.

Is that jus me or do other also rad the+ easier than the .Format .... ??
thanks,

JIM

"James" <no****@hypercon.netwrote in message
news:OY**************@TK2MSFTNGP06.phx.gbl...
Which is better, which is faster, which is easier etc... ?????

String.Format ( "yadda {0} yadda {1}", x, y )

"yadda" + x.ToString() + " yadda" + y.tostring();
My code has a mish mash of both and I am wondewring if I should
standardize or not ??
Thnanks,

JIM

Jun 25 '07 #6
YES !!! Thank you, this is exactly what I have always thought but wanted to
confirm with other developers. Glad to hear I am not alone on this. Syntax
highligting rocks for this too.

JIM

"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op.tuhpefjedj93y5@stone...
On Mon, 25 Jun 2007 20:18:41 +0200, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
James,

Using the + operator will be faster, as it will just pre-allocate the
string and copy the various elements together. With Format, the format
string is parsed, and that is going to require some overhead.

However, I do think that you should carefully consider the overhead
versus the readability/maintainability of the code, as I think that the
call
to Format is MUCH much better in this arena.

Of course, if you have a good number of these operations being
performed, then you could gain some performance by moving to the concat
operator (+).

In some cases I agree, but in many cases readability can be much improved by
not calling ToString(), in which case the OP's example narrows down to

string s = "yadda " + x + " yadda " + y;

which I would argue is more readable (syntax coloring will make it even more
readable) than

string s = string.Format("yadda {0} yadda {1}", x, y);
PS! If any of the parts of a string concatenation contains a string literal,
ToString() will automatically be called on other parts as well.

--
Happy coding!
Morten Wennevik [C# MVP]
Jun 25 '07 #7
James,

Curious, if your choice is strictly about readability, then what does it
matter what other developers think is readable or not readable? What
matters is the developers who are maintaining the codebase that you all are
working on. It's a subjective matter at that point.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"James" <no****@hypercon.netwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
YES !!! Thank you, this is exactly what I have always thought but wanted
to confirm with other developers. Glad to hear I am not alone on this.
Syntax highligting rocks for this too.

JIM

"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op.tuhpefjedj93y5@stone...
On Mon, 25 Jun 2007 20:18:41 +0200, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
>James,

Using the + operator will be faster, as it will just pre-allocate the
string and copy the various elements together. With Format, the format
string is parsed, and that is going to require some overhead.

However, I do think that you should carefully consider the overhead
versus the readability/maintainability of the code, as I think that the
call
to Format is MUCH much better in this arena.

Of course, if you have a good number of these operations being
performed, then you could gain some performance by moving to the concat
operator (+).


In some cases I agree, but in many cases readability can be much improved
by not calling ToString(), in which case the OP's example narrows down to

string s = "yadda " + x + " yadda " + y;

which I would argue is more readable (syntax coloring will make it even
more readable) than

string s = string.Format("yadda {0} yadda {1}", x, y);
PS! If any of the parts of a string concatenation contains a string
literal, ToString() will automatically be called on other parts as well.

--
Happy coding!
Morten Wennevik [C# MVP]

Jun 25 '07 #8
James wrote:
Readability, versus perfrmance.

The funny thing here is that, for me, the + operator is easier to read
than and brackets {0} etc... So, for me it looks like I get readability
and performance with the + operator.

Is that jus me or do other also rad the+ easier than the .Format .... ??
thanks,

JIM

"James" <no****@hypercon.netwrote in message
news:OY**************@TK2MSFTNGP06.phx.gbl...
>Which is better, which is faster, which is easier etc... ?????

String.Format ( "yadda {0} yadda {1}", x, y )

"yadda" + x.ToString() + " yadda" + y.tostring();
My code has a mish mash of both and I am wondewring if I should
standardize or not ??
Thnanks,

JIM
Hi Jim,

It does boil down to personal preference, but it also depends on the context
of your concatenation. You may have a complex string being built (such as
an SQL query, or some other stringified command that takes parameters
enclosed in quotes, brackets and whatnot - or multiple instances of the
same string), in which case I'd argue string.Format is more readable since:

1. It detaches the template of the string your creating from the parameters,
allowing for multiple uses of the same variable without duplicating
concatenation.

2. It provides a level of readability like that of a function call that
takes parameters, i.e. creating an SQL query.

--
Tom Spink
University of Edinburgh
Jun 25 '07 #9
Nicholas, what matters is that what is readble for ME also happens to be
the fastest, so for ME, I get the baset of both worlds, whereas for OTHERS,
they get what they believe is more readable, but they lose the speed factor.
So, my way appears to be the best way, that is if you apply logic to my
argument

JIM
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:uO**************@TK2MSFTNGP05.phx.gbl...
James,

Curious, if your choice is strictly about readability, then what does
it matter what other developers think is readable or not readable? What
matters is the developers who are maintaining the codebase that you all
are working on. It's a subjective matter at that point.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"James" <no****@hypercon.netwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>YES !!! Thank you, this is exactly what I have always thought but wanted
to confirm with other developers. Glad to hear I am not alone on this.
Syntax highligting rocks for this too.

JIM

"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op.tuhpefjedj93y5@stone...
On Mon, 25 Jun 2007 20:18:41 +0200, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
>>James,

Using the + operator will be faster, as it will just pre-allocate
the
string and copy the various elements together. With Format, the format
string is parsed, and that is going to require some overhead.

However, I do think that you should carefully consider the overhead
versus the readability/maintainability of the code, as I think that the
call
to Format is MUCH much better in this arena.

Of course, if you have a good number of these operations being
performed, then you could gain some performance by moving to the concat
operator (+).


In some cases I agree, but in many cases readability can be much improved
by not calling ToString(), in which case the OP's example narrows down to

string s = "yadda " + x + " yadda " + y;

which I would argue is more readable (syntax coloring will make it even
more readable) than

string s = string.Format("yadda {0} yadda {1}", x, y);
PS! If any of the parts of a string concatenation contains a string
literal, ToString() will automatically be called on other parts as well.

--
Happy coding!
Morten Wennevik [C# MVP]


Jun 25 '07 #10
The point I was trying to make is that you shouldn't be seeking
justification on subjective matters from those who's opinion on said
subjective matters doesn't matter.

In this case, the performance issue is not subjective, it is objective.

However, which is more readable, is subjective. If you are the only
person writing/maintaining the code, and that's what works for you, then
great, but if you are working in a team and others have to work with the
code that you write, then it is a team decision (or the team lead, or
whatever) as to what is more readable, not us.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"James" <no****@hypercon.netwrote in message
news:uR*************@TK2MSFTNGP04.phx.gbl...
Nicholas, what matters is that what is readble for ME also happens to be
the fastest, so for ME, I get the baset of both worlds, whereas for
OTHERS, they get what they believe is more readable, but they lose the
speed factor. So, my way appears to be the best way, that is if you apply
logic to my argument

JIM
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:uO**************@TK2MSFTNGP05.phx.gbl...
>James,

Curious, if your choice is strictly about readability, then what does
it matter what other developers think is readable or not readable? What
matters is the developers who are maintaining the codebase that you all
are working on. It's a subjective matter at that point.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"James" <no****@hypercon.netwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>>YES !!! Thank you, this is exactly what I have always thought but wanted
to confirm with other developers. Glad to hear I am not alone on this.
Syntax highligting rocks for this too.

JIM

"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op.tuhpefjedj93y5@stone...
On Mon, 25 Jun 2007 20:18:41 +0200, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:

James,

Using the + operator will be faster, as it will just pre-allocate
the
string and copy the various elements together. With Format, the format
string is parsed, and that is going to require some overhead.

However, I do think that you should carefully consider the overhead
versus the readability/maintainability of the code, as I think that the
call
to Format is MUCH much better in this arena.

Of course, if you have a good number of these operations being
performed, then you could gain some performance by moving to the concat
operator (+).

In some cases I agree, but in many cases readability can be much
improved by not calling ToString(), in which case the OP's example
narrows down to

string s = "yadda " + x + " yadda " + y;

which I would argue is more readable (syntax coloring will make it even
more readable) than

string s = string.Format("yadda {0} yadda {1}", x, y);
PS! If any of the parts of a string concatenation contains a string
literal, ToString() will automatically be called on other parts as well.

--
Happy coding!
Morten Wennevik [C# MVP]



Jun 25 '07 #11
On Mon, 25 Jun 2007 11:52:49 -0700, James <no****@hypercon.netwrote:
Nicholas, what matters is that what is readble for ME also happens to be
the fastest, so for ME, I get the baset of both worlds, whereas for
OTHERS,
they get what they believe is more readable, but they lose the speed
factor.
So, my way appears to be the best way, that is if you apply logic to my
argument
It is only the "best way" for you. That's what makes it subjective. Only
in very narrow, specific situations can performance be considered a high
enough priority to completely override readability.

Your paragraph above appears to be saying that others' idea of "readable"
is wrong because it leads to (very slightly) reduced performance. IMHO,
that's a false conclusion, since readability and performance are two
separate things and furthermore, readability is subjective while
performance is not.

As far as whether using concatenation is more readable than using string
formatting goes, that's completely in the eye of the beholder. But as
others have pointed out, there are good arguments in favor of using string
formatting. Bruce pointed out that anything that requires localization or
similar need to provide alternative formatting strings is going to be much
harder with concatenation, and I also agree that there are many cases
where being able to see the entire base string is more readable than
having to look at code that concatenates several string literals with data
interspersed.

You are welcome to your opinion that concatenation is more readable, but
don't think that it is "logical" to conclude that everyone else should
agree that it's more readable just because concatenation also happens to
perform better (however slightly).

Pete
Jun 25 '07 #12
James wrote:
In some cases I agree, but in many cases readability can be much improved by
not calling ToString(), in which case the OP's example narrows down to

string s = "yadda " + x + " yadda " + y;

which I would argue is more readable (syntax coloring will make it even more
readable) than
Yes, it's more readable, but also slower. If not all items are strings,
all items will instead be cast to object. Items that are value types
will therefore be boxed.

The generated code is actually:

string s = String.Concat((object)"yadda ", (object)x, (object)" yadda ",
(object)y);

The Concat method will then call the ToString method for all the
objects, even those that are already strings.
string s = string.Format("yadda {0} yadda {1}", x, y);
PS! If any of the parts of a string concatenation contains a string literal,
ToString() will automatically be called on other parts as well.
No, that is not correct. If any of the items is not a string, all items
are instead cast to object.

--
Göran Andersson
_____
http://www.guffa.com
Jun 26 '07 #13
On Jun 26, 9:43 am, Göran Andersson <g...@guffa.comwrote:
In some cases I agree, but in many cases readability can be much improved by
not calling ToString(), in which case the OP's example narrows down to
string s = "yadda " + x + " yadda " + y;
which I would argue is more readable (syntax coloring will make it evenmore
readable) than

Yes, it's more readable, but also slower.
Nope, I don't believe so.
If not all items are strings, all items will instead be cast
to object. Items that are value types will therefore be boxed.
How would you expect value types to be passed to String.Format without
boxing taking place?
The generated code is actually:

string s = String.Concat((object)"yadda ", (object)x, (object)" yadda ",
(object)y);

The Concat method will then call the ToString method for all the
objects, even those that are already strings.
Just the same as string.Format does...

Now, as for your claim that calling Format is faster, here's a
microbenchmark. I wouldn't be surprised to see results change
depending on the exact format etc, but it does show (at least on my
box) that string.Concat is faster in *some* situations:

using System;
using System.Diagnostics;

class Test
{
const int Iterations = 10000000;

static void Main()
{
int x = 5;

Stopwatch timer = Stopwatch.StartNew();
for (int i=0; i < Iterations; i++)
{
string y = "hello" + x + "there";
}
long ms = timer.ElapsedMilliseconds;
Console.WriteLine ("Concat took: {0}ms", ms);
GC.Collect();
timer = Stopwatch.StartNew();
for (int i=0; i < Iterations; i++)
{
string y = string.Format("hello {0} there", x);
}
ms = timer.ElapsedMilliseconds;
Console.WriteLine ("Format took: {0}ms", ms);
}
}

Results:

Concat took: 2528ms
Format took: 5536ms

Jon

Jun 26 '07 #14
On Jun 26, 10:02 am, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
which I would argue is more readable (syntax coloring will make it even more
readable) than
Yes, it's more readable, but also slower.

Nope, I don't believe so.
<snip>

Ah. Just reread your post. I assume now that you were *actually*
saying that

string y = "a" + b + "c";
is slower than
string y = "a" + b.ToString() + "c";

in which case I agree. Oops.

Jon

Jun 26 '07 #15
Jon Skeet [C# MVP] wrote:
On Jun 26, 10:02 am, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
>>>which I would argue is more readable (syntax coloring will make it even more
readable) than
Yes, it's more readable, but also slower.
Nope, I don't believe so.

<snip>

Ah. Just reread your post. I assume now that you were *actually*
saying that

string y = "a" + b + "c";
is slower than
string y = "a" + b.ToString() + "c";

in which case I agree. Oops.

Jon
Yes, that's what I meant. Sorry for not being specific about what I was
comparing against. :)

--
Göran Andersson
_____
http://www.guffa.com
Jun 26 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jonas Galvez | last post: by
5 posts views Thread by Thomas Philips | last post: by
11 posts views Thread by Frank Neuhaus | last post: by
16 posts views Thread by Don Starr | last post: by
7 posts views Thread by Brian Mitchell | last post: by
16 posts views Thread by Mark A. Sam | last post: by
9 posts views Thread by Terry Olsen | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.