Got confused about Strings

I read somewhere on this site the do's and dont's about strings...Because I use the + a lot I did run some tests....If you have a construct like this :

Eventlogentry = New Eventlogentry(“There was a failure executing receive location “ +  config.locationname + “. Please review the configuration“)

It doesn't matter  what you use cause it is only a single instance. You will see only a difference between string.format, string.concat and + if you do a zillion operations.

But remember this is only true if you only instantiate the resultstring. If you are adding to a string within a loop, use the stringbuilder.

Here are some timings :

Concat took        :660.9504
++++++ took        :650.936
Stringbuilder took :1432.0592
String format took :1071.5408

The size of the strings could have imact.  I did one million iterations and the difference between the stuff is minimal. So frankly if you use it only to construct a string to display somewhere... don't bother use whatevber you like best.

If you''re in a loop use the stringbuilder !

The code I used to produce these timings is in a reply on this post

Published Thursday, May 26, 2005 2:24 PM by Patrick Wellink
Filed under: ,

Comments

# re: Got confused about Strings

Thursday, May 26, 2005 2:25 PM by Patrick Wellink
Dim string1 As String = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
Dim string2 As String = "AAAAAAAAAAAAA"
Dim string3 As String = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"

Dim Sb As stringbuilder = New stringbuilder
Dim Result As String

Dim StartTime1 As DateTime = Now()
For x As Integer = 0 To 1000000
Result = String.Concat(string1, string2, string3)
Next
Dim EndTime1 As DateTime = Now()
Debug.WriteLine("Concat took :" + EndTime1.Subtract(StartTime1).TotalMilliseconds.ToString)

Dim StartTime2 As DateTime = Now()
For x As Integer = 0 To 1000000
Result = string1 + string2 + string3
Next
Dim EndTime2 As DateTime = Now()
Debug.WriteLine("++++++ took :" + EndTime2.Subtract(StartTime2).TotalMilliseconds.ToString)

Dim StartTime3 As DateTime = Now()
For x As Integer = 0 To 1000000
Sb.Length = 0
Sb.Append(string1)
Sb.Append(string2)
Sb.Append(string3)
Result = Sb.ToString
Next
Dim EndTime3 As DateTime = Now()
Debug.WriteLine("Stringbuilder took :" + EndTime3.Subtract(StartTime3).TotalMilliseconds.ToString)

Dim StartTime4 As DateTime = Now()
For x As Integer = 0 To 1000000
Result = String.Format("(1)(2)(3)", string1, string2, string3)
Next
Dim EndTime4 As DateTime = Now()
Debug.WriteLine("String format took :" + EndTime4.Subtract(StartTime4).TotalMilliseconds.ToString)

# re: Got confused about Strings

Thursday, May 26, 2005 3:08 PM by Patrick Wellink
You beat me to it! I ran the same tests yesterday and got similar results.

# re: Got confused about Strings

Thursday, May 26, 2005 3:50 PM by Patrick Wellink
Ok i just gat tired of use this and use that....
only in a loop where a big string is constructed it matters

# re: Got confused about Strings

Thursday, May 26, 2005 3:56 PM by Patrick Wellink
Sorry, maar dit is wat we noemen: "een open deur intrappen" ;)

# re:Got confused about Strings

Wednesday, June 15, 2005 9:06 AM by TrackBack
Got confused about Stringsooeess