How to speed up access to the RichTextBox Lines property | ||
| ||
| ... private void Form1_Load(object sender, System.EventArgs e) { Cursor.Current = Cursors.WaitCursor; StringBuilder Buffer = new StringBuilder(""); for (int i = 1; i <= 1000; i++) { if (i > 1) { Buffer.Append(Environment.NewLine); } Buffer.Append("This is line number " + i.ToString()); } TheRichTextBox.Text = Buffer.ToString(); Cursor.Current = Cursors.Arrow; } private void ForLoopButton_Click(object sender, System.EventArgs e) { Cursor.Current = Cursors.WaitCursor; int Len = 0; int Start = Environment.TickCount; for (int i = 0; i < TheRichTextBox.Lines.Length; i++) { Len += TheRichTextBox.Lines[i].Length; } int ElapsedTime = Environment.TickCount - Start; ResultsTextBox.Clear(); ResultsTextBox.Text = "for loop Elapsed time = " + ((double) ElapsedTime / (double) 1000.0).ToString() + " seconds Result = " + Len.ToString(); Cursor.Current = Cursors.Arrow; } private void ForEachLoopButton_Click(object sender, System.EventArgs e) { Cursor.Current = Cursors.WaitCursor; int Len = 0; int Start = Environment.TickCount; foreach (String Line in TheRichTextBox.Lines) { Len += Line.Length; } int ElapsedTime = Environment.TickCount - Start; ResultsTextBox.Clear(); ResultsTextBox.Text = "foreach loop Elapsed time = " + ((double) ElapsedTime / (double) 1000.0).ToString() + " seconds Result = " + Len.ToString(); Cursor.Current = Cursors.Arrow; } ... | ||
| | ||
| |