This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
When to use StringBuilder?
I am working hard in performance gaining in the Enterprise project. Our solution architect suggesting to convert all string declarions in to stringbuilder If even one append is there to that string variable. for ex: str += str2;
I know, if we are modiying string many times (ex: many appends) can be time consuming to create new string objects. we should go to stringbuilder.
Now Question is -
Creating string builder object (without specifying default size) Vs one time append in to string variablein respect of performance improvement.
There are a number of articles out there that answer this question for you. Here is the first one I came across:
http://www.codeproject.com/Articles/6771/String-Vs-StringBuilder-C
Basically, the answer is that no, you probably shouldn't use stringbuilder for instances where you only append text once or twice. It is really for when you are doing so repeatedly.
Related
This question already has answers here:
Counting the occurrences of every duplicate words in a string using dictionary in c# [closed]
(3 answers)
Closed 6 years ago.
I am making something like, the user will input any url and the text will be obtained.
The text will then be parsed and the words will be counted.
I am currently reading this article from microsoft:
https://msdn.microsoft.com/en-us/library/bb546166.aspx
I can now get the text and i am currently trying to think of an efficient way to count every words.
The article example required a search data but i need to search every word and not a specific word.
Here is what i am thinking:
get the text and convert it to string
split them (delimiters) and store in array
loop through the array then check every occurrences of it.
would this be efficient?
Using Linq
If you have a small amount of data can just do a split on spaces, and create a group
var theString = MethodToGetStringFromUrl(urlString);
var wordCount = theString
.Split(' ')
.GroupBy(a=>a)
.Select(a=>new { word = a.Key , Count = a.Count() });
see fiddle for more a working copy
Some Experiments and Results
Messed around in .net fiddle a little bit and using Regexs actually decreased the performance and increased the amount of memory used see here to see what I am talking about
Other alternative
Because you are getting the request from a Url it might be more performant to search inside of the stream before converting it to a string and then performing the search
Don't optimize unless you need to
Why do you need to find a performant way to do this count? Have you run into any issues or just think you will, a good rule of thumb is generally not to prematurely optimize, for more information check out this good question on the topic : When is optimisation premature?
This question already has answers here:
How to use StringBuilder wisely?
(3 answers)
Closed 7 years ago.
What is more efficient in performance to prepend a string to another?
Using the StringBuilder.Insert method or the string.Concat method?
messageString.Insert(0, prependedString);
or
string.Concat(prependedString, messageString);
In my case the message string is relatively big, the prepended string is short.
string.Concat is the fastest method if the number of items is fixed. This statement holds true in all cases. It does not matter how long the strings are.
string.Concat calculates the final string size and then copies over the bits into a freshly allocated string. It cannot be done any faster.
In fact, you should write a + b instead of calling Concat (if that is possible in the specific situation).
for huge strings use string builder
False. Why would that be the case?!
If you're concating more than two strings use StringBuilder
False. If the number is fixed, use Concat. StringBuilder gains you nothing but adds overhead.
the answer depends on how many strings you are concatenating, and how big they are
False. The algorithm that I described above is always the fastest possible solution.
The myths around StringBuilder are an amazing variety. If you understand how both options work internally you can answer all these questions yourself. I did not study and memorize all these answers. I generate them from my understanding of internals.
This is a duplicate of How to use StringBuilder Wisely you can read my full answer over there, in short:
Concat function is faster than working with StringBuilder for the number of strings entering the function is known.
This question already has answers here:
String output: format or concat in C#?
(32 answers)
Why use String.Format? [duplicate]
(7 answers)
Closed 9 years ago.
Why shouldn't we simply use
string s=product.Name+" has been saved";
instead of:
string s=string.Format("{0} has been saved", product.Name);
One naive reason would be that it helps to prevent exactly the string formatting issue that you've presented in your original (unedited) question i.e.
string s=product.Name+"has been saved";
requires an extra space. The format method aids readability.
You could do that, no one say that you cannot. But mainly for readability, the second approach is prefered. It's even more obvious as soon as you concat more than 2 strings, it gets really messy, hard to read and mantain.
If you have many strings that you want to add, each + operation create new string.
For adding many strings you can use StringBuilder Class or String.Format
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Stringbuilder vs String.Concat
As I understand it, String are immutable, so if you do "one " + "two", rather than reusing the memory of these two strings, a new string is created with value "one two". This becomes a problem if you do something like:
String output = "";
for (int i = 0; i < 20; i++){
output = output + ii.toString();
}
Because 19 (or 39?) strings are created and discarded to produce the final desired output.
String builders are mutable, so you can add strings together efficiently, and then convert them into a string when you are done.
I assume there is some overhead in creating string builders, and also the clever compilers used today optimize away lots of string addition problems. So my question is: how many strings do I need to add together before it becomes more efficient to use a StringBuilder instead? Or are my assumptions incorrect?
An answer in this question - Does StringBuilder use more memory than String concatenation? - states 'a few dozen' is the threshold.
And linked from another answer in that question is this link - http://www.codinghorror.com/blog/2009/01/the-sad-tragedy-of-micro-optimization-theater.html - which concludes with "it doesn't matter"!
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
.NET String.Format() to add commas in thousands place for a number
Hi Guys,
I have got 26750 in my string variable, something like below:
string str = "26750";
Now before showing on page, I want it to be converted into "26,750" format using c#. This value can increase as well as decrease also according to the result, so my format should work in both the cases.
Please suggest!
EDIT:
As I have written I have got string type value in my variable, I am trying with below code, but it is not working for me.
spnMiles.InnerHtml = String.Format("{0:n}", Miles);
It is not changing to the number format.
Please suggest!
This question should answers yours:
.NET String.Format() to add commas in thousands place for a number