Click here to Skip to main content
15,891,136 members
Home / Discussions / C#
   

C#

 
GeneralRe: Discussion : Can we say Overloading as polymorphism Pin
Cracked-Down8-Apr-09 23:20
Cracked-Down8-Apr-09 23:20 
GeneralRe: Discussion : Can we say Overloading as polymorphism Pin
dojohansen9-Apr-09 3:04
dojohansen9-Apr-09 3:04 
QuestionString manipulation with large strings [modified] Pin
Harvey Saayman8-Apr-09 22:17
Harvey Saayman8-Apr-09 22:17 
AnswerRe: String manipulation with large strings Pin
Rob Philpott8-Apr-09 22:52
Rob Philpott8-Apr-09 22:52 
GeneralRe: String manipulation with large strings Pin
Harvey Saayman8-Apr-09 23:13
Harvey Saayman8-Apr-09 23:13 
GeneralRe: String manipulation with large strings Pin
Rob Philpott8-Apr-09 23:24
Rob Philpott8-Apr-09 23:24 
GeneralRe: String manipulation with large strings Pin
Harvey Saayman8-Apr-09 23:38
Harvey Saayman8-Apr-09 23:38 
GeneralRe: String manipulation with large strings Pin
dojohansen8-Apr-09 23:58
dojohansen8-Apr-09 23:58 
I for one have difficulty believing that what you say happens actually does happen.

But, even if your code is correct and the string you're interested in should eventually be extracted from it you should never ever manipulate large strings in .NET. They are immutable. And that means that whenever you modify a string you are in fact allocating a new string and copying all the not-modified characters from the original. That's not cool.

For an extreme display of the difference in performance, try running this code in a console.

Stopwatch w;

static void time(string msg)
{
   Console.WriteLine(msg + ", elapsed time = " + w.Elapsed.ToString());
   w = Stopwatch.StartNew();
}

static void Main(string[] args)
{
   int count = 1000*1000; 
   string s = "";
   StringBuilder sb = new StringBuilder();
   
   w = Stopwatch.StartNew();
   for (int i=0; i < count; i++) s += ".";
   time("Using string");
   for (int i=0; i < count; i++) sb.Append(".");
   time("Using StringBuilder");
}


You may find you wish to adjust the count so that the "string" implementation will ever finish, although the StringBuilder will do this pretty quickly.

Obviously it is a very extreme example - the statement s += "."; hides an inner loop copying all the characters of the old s into the new s. Even worse, it puts huge pressure on the memory manager as it allocates a million strings (count), and these eventually become huge, forcing the GC to collect and reallocate, collect and reallocate all the time. You will see that with small strings both methods run in a short time, but percentage-wise the StringBuilder will always be a lot faster.

The way the StringBuilder does this internally is by representing the string as a char[] and keeping track of which chars actually have valid data in them. Just like an ArrayList it doubles in size when it reallocates, so starting from 16 (you'd get slightly better performance if you passed the correct capacity to the StringBuilder constructor as then it wouldn't need to reallocate ever) it must reallocate just fourteen times to get to a million, against one million for the string method.
GeneralRe: String manipulation with large strings Pin
Harvey Saayman9-Apr-09 0:30
Harvey Saayman9-Apr-09 0:30 
GeneralRe: String manipulation with large strings Pin
dojohansen9-Apr-09 1:14
dojohansen9-Apr-09 1:14 
GeneralRe: String manipulation with large strings Pin
dojohansen9-Apr-09 1:25
dojohansen9-Apr-09 1:25 
JokeRe: String manipulation with large strings Pin
dojohansen9-Apr-09 1:37
dojohansen9-Apr-09 1:37 
Questionhow to select the text from the dropdownlist Pin
mdazeemuddin8-Apr-09 22:03
mdazeemuddin8-Apr-09 22:03 
QuestionPenel in Status Bar [modified] Pin
Sajjad Leo8-Apr-09 21:16
Sajjad Leo8-Apr-09 21:16 
AnswerRe: Penel in Status Bar Pin
DaveyM698-Apr-09 22:09
professionalDaveyM698-Apr-09 22:09 
GeneralRe: Penel in Status Bar Pin
Sajjad Leo8-Apr-09 23:19
Sajjad Leo8-Apr-09 23:19 
GeneralRe: Penel in Status Bar Pin
DaveyM699-Apr-09 0:11
professionalDaveyM699-Apr-09 0:11 
GeneralRe: Penel in Status Bar Pin
Sajjad Leo12-Apr-09 20:28
Sajjad Leo12-Apr-09 20:28 
QuestionMedia player control Pin
yesu prakash8-Apr-09 20:44
yesu prakash8-Apr-09 20:44 
AnswerRe: Media player control Pin
12Code8-Apr-09 20:52
12Code8-Apr-09 20:52 
QuestionHelp to me to find simple method for days of month Pin
M Riaz Bashir8-Apr-09 20:30
M Riaz Bashir8-Apr-09 20:30 
AnswerRe: Help to me to find simple method for days of month Pin
DaveyM698-Apr-09 20:37
professionalDaveyM698-Apr-09 20:37 
GeneralRe: Help to me to find simple method for days of month Pin
M Riaz Bashir8-Apr-09 20:39
M Riaz Bashir8-Apr-09 20:39 
QuestionOracle.DataAccess.Client - DbProviderFactories.GetFactory [modified] Pin
devvvy8-Apr-09 20:25
devvvy8-Apr-09 20:25 
AnswerRe: Oracle.DataAccess.Client - DbProviderFactories.GetFactory Pin
devvvy9-Apr-09 16:00
devvvy9-Apr-09 16:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.