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 17 15294
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.
"ramadu" <tn*@newsgroups.nospam> schrieb im Newsbeitrag
news:%2******************@TK2MSFTNGP04.phx.gbl... 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
I realize that but does .net do any compiler optimizations if
string.Format is used?
- 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.
"ramadu" <tn*@newsgroups.nospam> schrieb im Newsbeitrag news:%2******************@TK2MSFTNGP04.phx.gbl... 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
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
: 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
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" <tn*@newsgroups.nospam> wrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl... 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
ramadu <tn*@newsgroups.nospam> wrote: 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.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... 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.
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" <tn*@newsgroups.nospam> schrieb im Newsbeitrag
news:44**************@newsgroups.nospam... I realize that but does .net do any compiler optimizations if string.Format is used?
- 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.
"ramadu" <tn*@newsgroups.nospam> schrieb im Newsbeitrag news:%2******************@TK2MSFTNGP04.phx.gbl... 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
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?
ramadu wrote: 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
Mark Wilden wrote: "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message news:MP************************@msnews.microsoft.c om...
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.
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
"JimD" <Ji*@keeliegirl.dyndns.org> wrote in message
news:Ji******************@tornado.tampabay.rr.com. .. // 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 <Ma********@newsgroups.nospam> wrote: "JimD" <Ji*@keeliegirl.dyndns.org> wrote in message news:Ji******************@tornado.tampabay.rr.com. ..
// 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.
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.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... 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.)
Its mostly for select statements in a DataTable.
- Sri
: 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?
ramadu wrote: 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
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" <Oc******@gmail.com> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com... 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.
ramadu wrote: Its mostly for select statements in a DataTable.
- Sri
: 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?
ramadu wrote: 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Hector A |
last post by:
Hi
I'm trying to convert a string that already looks like a date to a
date that I can use when I pass it from java to the database. I
receive the date in format yyyy-mm-dd and I need it to be a...
|
by: Dennis Westermann |
last post by:
Hi,
does anybody know, how i can attach a simple apostroph ( ' ) to a string.
I want to create a string with concat(..), which starts and ends with a
simple apostroph.
The output should be:...
|
by: jez123456 |
last post by:
Hi I'm attempting to pass a string into another string but I get an error
This code work ok
jro.CompactDatabase(@"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Compact.mdb",...
|
by: Dominique Vandensteen |
last post by:
after the very small & vs string.format discussion I did some speed tests...
loop of 1.000.000 concatenations of 5 public string variables in a class
gave following results:
result = a & b...
|
by: Trint Smith |
last post by:
Ok,
I have a webform that has these checkboxes:
1. something
2. something else
3. and something else
When the user clicks on the checkbox, I want all of the selections to go
into a textbox...
|
by: Ben |
last post by:
Hi
I have a string which is at least 4 characters but can be more which I need
to pad, with zeros, to a full length of 15 after the 3rd charactor.
Eg '1234' to '123000000000004'
How can I...
|
by: Leonel Gayard |
last post by:
Hi all,
I had to write a small script, and I did it in python instead of
shell-script. My script takes some arguments from the command line,
like this.
import sys
args = sys.argv
if args ==...
|
by: doubts |
last post by:
Hi all,
I am trying to convert my bulk of code from VC++ 6.0 to VC++.Net.
when using std::string type variable, the application causes exception
at one instance and does not cause an exception at...
|
by: James |
last post by:
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...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |