StringBuilder performance

  • Thread starter Thread starter JC Voon
  • Start date Start date
J

JC Voon

Hi all:

If i'm not wrong, StringBuilder should faster than string concat using
&.
But the following code:

1:
Dim s as string
For i As Integer = 1 To 1000000
s = "11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111"
Next

2:
Dim sb As StringBuilder = New StringBuilder
For i As Integer = 1 To 1000000
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
Next

Why stringbuiler is slower ?

Thanks
JCVoon
 
Probably because the optimiser concatenates the string at "compile" time and
is just assigning it at run-time (if even that). If you tried that with
variable strings, you'll probably find it's slower.
 
JC Voon said:
If i'm not wrong, StringBuilder should faster than string concat using
&.
But the following code:

1:
Dim s as string
For i As Integer = 1 To 1000000
s = "11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111"
Next

['StringBuilder' sample]
Why stringbuiler is slower ?

In this particular case, the compiler will find out that the text you are
assigning to 's' is a single constant string, and thus the compiler will
emit a single string literal. Consequently no concatenations are performed
at tuntime. Note that this is only the case when concatenating constant
strings.
 
You're not concatenating anything. The compiler recognizes your string
s as a constant and does all the concatenating once at compile time.
If you look at the IL that is emitted, you will see this.
 
Hi:

Thanks for the reply.
After modify my code, i can see the different.



Dim tStart As Double

Dim sb As StringBuilder = New StringBuilder
tStart = DateAndTime.Timer
For i As Integer = 1 To 5000
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
sb.Append("11111111111111111111111111111111")
Next
System.Windows.Forms.MessageBox.Show(CStr(DateAndTime.Timer -
tStart))



Dim s As String = ""
tStart = DateAndTime.Timer
For i As Integer = 1 To 5000
s &= "11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111" & _
"11111111111111111111111111111111"
Next
System.Windows.Forms.MessageBox.Show(CStr(DateAndTime.Timer -
tStart))



Thanks
JCVoon
 
Back
Top