473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

StringBuilder performance

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
Nov 21 '05 #1
4 1204
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" <jc*******@yahoo.com> wrote in message
news:42***************@msnews.microsoft.com...
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

Nov 21 '05 #2
"JC Voon" <jc*******@yahoo.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3
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.

Nov 21 '05 #4
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(DateAndT ime.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(DateAndT ime.Timer -
tStart))

Thanks
JCVoon
Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
14
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
12
by: Richard Lewis Haggard | last post by:
I thought that the whole point of StringBuilder was that it was supposed to be a faster way of building strings than string. However, I just put together a simple little application to do a...
26
by: Hardy Wang | last post by:
Hi all, I know it is better to handle large string with a StringBuilder, but how does StringBuilder class improve the performance in the background? Thanks! -- WWW:...
3
by: Morgan Cheng | last post by:
In P/Invoke situation, If some *out* parameter is LPWSTR, I can use string or StringBuilder. However, there is one problem about StringBuilder. By default, its Capacity is 16. If the returned...
5
by: pantagruel | last post by:
Hi, It is generally stated that stringbuilder should be used instead of just concatenating strings with the plus operator. That's fine enough what I'm wondering in cases I have: String S =...
34
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.