|
-
Jul 11th, 2007, 05:27 AM
#1
Thread Starter
Addicted Member
[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
-
Jul 11th, 2007, 06:15 AM
#2
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());
-
Jul 11th, 2007, 06:34 AM
#3
Thread Starter
Addicted Member
Re: [2.0] repetitive string modification slowing down application...
 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|