String.Concat vs String.Format

  • Thread starter Thread starter ramadu
  • Start date Start date
R

ramadu

I know its a sin to use strings, lets skip that part...

Which of these is faster and uses less memory?

String.Format("SomeValue='{0}'", m_Value);

or

String.Concat("SomeValue='", m_Value, "'");

- ramadu
 
If you think a bit about it you 'll get the answer.

String.Concat is made to do exactly this where it get the name from, whereas
String.Format can do lots of stuff and has to scan the string for format
specifiers first.
 
I realize that but does .net do any compiler optimizations if
string.Format is used?

- ramadu

:
 
Hello ramadu,

If u use reflector you can see that String.Format use StringBuilder class
and calls its AppendFormat method that is pretty rich.
String,Contat uses string.FastAllocateString and FillStringChecked methods

I'd assume that String.Concat is more fastest case

r> I know its a sin to use strings, lets skip that part...
r>
r> Which of these is faster and uses less memory?
r>
r> String.Format("SomeValue='{0}'", m_Value);
r>
r> or
r>
r> String.Concat("SomeValue='", m_Value, "'");
r>
r> - ramadu
r>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
But in that case String.Concat will be costly in terms of memory as it
will create multiple immutable strings...

- ramadu

:
 
Just for the sake of discussion. All your variables has a scope. How many
variable can you accumulate till you quit the program? So, to me I would say
I use whichever way I feel is comfortable to me.

chanmm
 
ramadu said:
I know its a sin to use strings, lets skip that part...

In what way? Using strings is part of every day life. Is there some
particular use you're thinking of which isn't a good idea, like using
values directly in SQL statements?
Which of these is faster and uses less memory?

String.Format("SomeValue='{0}'", m_Value);

or

String.Concat("SomeValue='", m_Value, "'");

Well, the Concat call is faster, *but* it's unlikely to ever be
significant. The more important question is "Which of these is clearer,
and more readable?" There, the answer is String.Format IMO. On the
other hand, your Concat call could be made easier to read too:

string x = "SomeValue='" + m_Value + "'";

That will compile to the same code, and is fairly readable. I'd still
use String.Format though, I think.
 
Well, the Concat call is faster, *but* it's unlikely to ever be
significant. The more important question is "Which of these is clearer,
and more readable?" There, the answer is String.Format IMO.

I don't know -- when you're just concatenating strings, + (or Concat) is
more readable, IMO.

///ark
 
what do you mean with "compiler optimizations"?

ramadu said:
I realize that but does .net do any compiler optimizations if string.Format
is used?

- ramadu

:
 
The Concat would be slightly faster and memory efficient, but if you are
trying to optimize your code you are probably looking in the wrong place.

What are you going to use the string for?
 
Mark said:
I don't know -- when you're just concatenating strings, + (or Concat) is
more readable, IMO.

///ark

I think it depends on what you are doing:

version #1:

string sql = "";
sql = "Insert Into Foo (custid, salary, bar, snoopy, bugs)"
+ " values (" + m_custid + ", " + m_sal + ", '" + m_bar + "', '"
+ m_snoopy + "', '" + m_bugs + "')";



version #2

// this string would probably be defined somewhere else
string sqlIns = Insert Into Foo (custid, salary, bar, snoopy, bugs)"
+ " values ({0}, {1}, '{2}', '{3}', '{4}')";

string sql = String.Format(sqlIns, m_custid, m_sal,
m_bar, m_snoopy, m_bugs);

I think version #2 is the cleanest.

Jim
--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
You roll an 18 in Dex and see if you
don't end up with a girlfriend
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
JimD
Central FL, USA, Earth, Sol
 
// this string would probably be defined somewhere else
string sqlIns = Insert Into Foo (custid, salary, bar, snoopy, bugs)"
+ " values ({0}, {1}, '{2}', '{3}', '{4}')";

string sql = String.Format(sqlIns, m_custid, m_sal,
m_bar, m_snoopy, m_bugs);

I think version #2 is the cleanest.

I agree, but that isn't concatenation.

///ark
 
Mark Wilden said:
I agree, but that isn't concatenation.

Version 1 was though. You said:

"I don't know -- when you're just concatenating strings, + (or Concat)
is more readable, IMO."

Tha alternative to the Format call above was:

string sql = "";
sql = "Insert Into Foo (custid, salary, bar, snoopy, bugs)"
+ " values (" + m_custid + ", " + m_sal + ", '" + m_bar + "', '"
+ m_snoopy + "', '" + m_bugs + "')";

That's just concatenating strings, which would suggest that you'd
prefer that to the Format version.
 
string sql = "";
sql = "Insert Into Foo (custid, salary, bar, snoopy, bugs)"
+ " values (" + m_custid + ", " + m_sal + ", '" + m_bar + "', '"
+ m_snoopy + "', '" + m_bugs + "')";

That's just concatenating strings

Yes, you're right.

When pasting two strings together, I think + is simplest and clearest. When
formatting a long mixture of string constants and string variables, I think
Format is simpler. People talk about the former as concatenation and the
latter as formatting.

However, it's just my personal preference; I don't think it makes too much
difference either way. I certainly wouldn't worry about Format taking a
couple more ticks to execute.

(BTW, as long as we're on the subject, your StringBuilder page has let me
astound and amaze more than one of my colleagues who shun + like vampires.)
 
This kind of discussion is the result of too much pre-optimization and
also trying to steadfastly adhere to such overly generalized rules as
"concatenation is bad!".

For simple log statements, or when you're concatentation is limited,
it's fine.

It's when you are concatenating strings (especially long strings) in
loops, or doing lots of editing to the string, when you should look for
alternatives.
 
Matthew Brown said:
This kind of discussion is the result of too much pre-optimization and
also trying to steadfastly adhere to such overly generalized rules as
"concatenation is bad!".

For simple log statements, or when you're concatentation is limited,
it's fine.

It's when you are concatenating strings (especially long strings) in
loops, or doing lots of editing to the string, when you should look for
alternatives.

Actually, we've both read
<http://www.yoda.arachsys.com/csharp/stringbuilder.html>. :)
 
Then you shouldn't use either method. Use parameters instead.

Parameters are somewhat slower than putting the query together yourself,
but it's easier and safer. As the database call is going to use 99% of
the execution time of the routine anyway, you are looking in the wrong
place if you are trying to speed it up by optimizing the creation of the
query.

If you want to put together the queries yourself anyway, you should
escape the characters that the database uses in strings. For MySQL, for
an example, it is done like this:

"SomeValue='" + m_Value.Replace("\\","\\\\").Replace("'","\\'") + "'"

If you don't, you will encounter problems whenever someone uses any of
those characters. If you are lucky you only end up with an error
message. If you are unlucky someone used it for SQL injections, and all
your data is stolen and/or deleted.
 

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

Back
Top