[RESOLVED] [2.0] repetitive string modification slowing down application...
Ok, so im running a loop, and every iteration of this loop adds some text to an "output" textbox, which can be later saved as a log file...
Ok, say i have the following code...
Code:
for (int i = 0; i < 1000; i++) {
output.Text = output.Text + "some big long string here, much longer than this text...\r\n";
}
as i increases, it takes marginally longer each iteration to add the text... in fact, the time it takes is exponential...which makes sense..
what im wondering is, is there like an Append() method, or something similar...or how would i go about creating one, as this is a really slow part of my app. and i just know that it can be coded much more efficently, i just havent nutted out how.
thanks for the help in advance...
Cheers,
Justin
Re: [2.0] repetitive string modification slowing down application...
String is an immutable object and everytime you concatenate or append something, a new memory is allocated, slowing down the process.
To append large data togther, you must use object of System.Text.StringBuilder class.
Code:
System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
for (int i = 0; i < 1000; i++) {
stringBuilder.Append("some big long string here, much longer than this text...\r\n");
}
//display here using
Console.WriteLine(stringBuilder.ToString());
Re: [2.0] repetitive string modification slowing down application...
Quote:
Originally Posted by Harsh Gupta
String is an immutable object and everytime you concatenate or append something, a new memory is allocated, slowing down the process.
To append large data togther, you must use object of System.Text.StringBuilder class.
Code:
System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
for (int i = 0; i < 1000; i++) {
stringBuilder.Append("some big long string here, much longer than this text...\r\n");
}
//display here using
Console.WriteLine(stringBuilder.ToString());
Hi, and thanks for the reply, that is exactly what i was looking for..
Cheers, and thanks again...
Justin