Reputation: 21
In my project I need to output large list of string to control that is similiar to windows notepad. Right now Im using TextBox.
for(int i = 0; i< result.Count;i++)
{
textbox.Text += result[i].ToString() + Environment.NewLine;
}
If result count = 3000 then this loop lasts 13 second on my pc, this is definetely too slow, so what I should use(or how to configure Textbox?) for faster output?
Upvotes: 0
Views: 1398
Reputation: 61379
To a certain extent you are always going to take a performance hit if you are enumerating a very large collection. However, you can avoid a lot of string concatenation (which is very slow and memory intensive), and along with it having to assign the control text every iteration of your loop by using built-in string creation methods before assignment. An easy way in this case would be String.Join
textbox.Text = String.Join(Environment.NewLine, result);
Using a StringBuilder
would also be more efficient:
StringBuilder builder = new StringBuilder();
for(int i = 0; i< result.Count;i++)
{
builder.AppendLine(result[i]);
}
textbox.Text = builder.ToString();
Upvotes: 3