|
|
|||
| H o m e | |||
|
|
O p t i m i z e S t r i n g P e r f o r m a n c e By Michael McIntyre
Code that wastefully makes and discards strings is slow and inefficient. Examine the three examples below. All three produce the same result but the first executes in 6.4 seconds, the second in 3.3 seconds, and the third in .7 second.
EXAMPLE ONE
Dim startTime, endTime As DateTime Dim reportString As String startTime = Now Dim i As Integer For i = 1 To 5000 reportString &= i.ToString reportString &= Now.ToLongTimeString Next i endTime = Now MessageBox.Show("Concatenation took " & endTime.Subtract(startTime).TotalMilliseconds & " milliseconds.")
Result: Concatenation took 6359 milliseconds.
EXAMPLE TWO
Dim startTime, endTime As DateTime Dim reportString As String startTime = Now Dim i As Integer For i = 1 To 5000 reportString &= i.ToString & Now.ToLongTimeString Next i endTime = Now Console.WriteLine("Concatenation took " & endTime.Subtract(startTime).TotalMilliseconds & " milliseconds.")
Result: Concatenation took 3264 milliseconds.
EXAMPLE THREE
Dim startTime, endTime As DateTime Dim reportString As String Dim aStringBuilder As New StringBuilder(100000) startTime = Now Dim i As Integer For i = 1 To 5000 aStringBuilder.Append(i.ToString & Now.ToLongTimeString) Next i reportString = aStringBuilder.ToString endTime = Now Console.WriteLine("Concatenation took " & endTime.Subtract(startTime).TotalMilliseconds & " milliseconds.")
Result: Concatenation took 70 milliseconds.
Example one is slowest. The code inside the loop wastefully creates and discards strings.
reportString &= i.ToString reportString &= Now.ToLongTimeString
Strings are immutable – they can not be changed. Each line above actually makes a new string that is assigned to the reportString variable leaving the old one for garbage collection. This means that each pass through the loop creates two new strings and discards two old strings.
Example two cuts execution time almost in half.
reportString &= i.ToString & Now.ToLongTimeString
Concatenation inside the loop is now done with one line of code. Now each pass through the loop creates one new string and discards one old string.
Example three cuts execution time to just 11% of example one’s execution time. It uses the .NET Framework’s StringBuilder class. The StringBuilder class should be used in code that performs a large number of concatenations or other alterations. The StringBuilder class in the System.Text namespace supports a mutable string, which retains the same instance after modification. To use it, you declare a string variable with the StringBuilder data type instead of String. You can then call its Append, Insert, Remove, and Replace methods to manipulate the string. Summary When performing a large number of concatenations or other string alterations you should be choosing to use the StringBuilder object over the String object. However, if you do not expect to manipulate a string very often, String is a better choice. This is because StringBuilder has one-time overhead that String does not. At creation time, the StringBuilder constructor takes more time than the String constructor. Learn more about the StringBuilder clase by clicking here. |
||
|
Copyright © 2001-2003 aZ Software Developers. All rights reserved. |
|||