Stringbuilder and concatentaion question

  • Thread starter Thread starter Joe Fallon
  • Start date Start date
J

Joe Fallon

Is concatentaion inside of a Stringbuilder "evil"?

Which is the preferred syntax below?

Syntax #1
Dim sb As New StringBuilder
sb.Append("Line 1" & vbCrLf)
sb.Append("Line 2" & vbCrLf)
sb.Append("Line 3" & vbCrLf)

Syntax #2
Dim sb As New StringBuilder
sb.Append("Line 1")
sb.Append(vbCrLf)
sb.Append("Line 2")
sb.Append(vbCrLf)
sb.Append("Line 3")
sb.Append(vbCrLf)
 
* "Joe Fallon said:
Is concatentaion inside of a Stringbuilder "evil"?

Which is the preferred syntax below?

Syntax #1
Dim sb As New StringBuilder
sb.Append("Line 1" & vbCrLf)
sb.Append("Line 2" & vbCrLf)
sb.Append("Line 3" & vbCrLf)

Syntax #2
Dim sb As New StringBuilder
sb.Append("Line 1")
sb.Append(vbCrLf)
sb.Append("Line 2")
sb.Append(vbCrLf)
sb.Append("Line 3")
sb.Append(vbCrLf)

I vote for #1, because the compiler will remove the concatenation and
store the concatenated string literal instead ("Line 1\r\n"). So, #1
will have better performance because there are fewer method calls.
 
You can do concatenation inside the argument that you pass to StringBuilder.

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
 
Joe,
I prefer #2, as I am not creating a temporary string for each append.

However in your specific example, ("Line 1" & vbCrLf) is a constant and will
not create a temporary string (the compiler will concat the strings at
compile time). Which also means in your specific example you should do a
single Append instead of 3 appends!
Syntax #1
Dim sb As New StringBuilder
sb.Append("Line 1" & vbCrLf & "Line 2" & vbCrLf & "Line 3" & vbCrLf)

Where as the follow will create temporary strings, possibly negating the
"benefit" of using the StringBuilder in the first place:

Public Sub GetString(line1 As String, line2 As String, line3 As String)
Dim sb As New StringBuilder
sb.Append(line1 & vbCrLf)
sb.Append(line2 & vbCrLf)
sb.Append(line3 & vbCrLf)
End Sub

Also without a Loop or a significant number of concatenations, I would
consider if I really needed to StringBuilder or not...

For example, if I only had the 3 variables, I would consider using
String.Format instead or even String.Concat.

Public Sub GetString(line1 As String, line2 As String, line3 As String)
Const format As String = "{0}" & vbCrLf & "{1}" & vbCrLf & "{2}"
Dim s As String = String.Format(format, line1, line2, line3)
End Sub

The following article discusses when to use multiple Appends with the
StringBuilder:

http://msdn.microsoft.com/architecture/default.aspx?pull=/library/en-us/dnpag/html/scalenet.asp

Hope this helps
Jay
 
Hi Joe,

You can choose for #1, however #2 is faster.

(Execpt of course that you want to do as what Herfried says and use the
Stringbuilder to make this constant Dim myconst as String = "Line1" &
VbCrlF & "Line2" & VbCrlF & "Line3" & VbCrlF, than is this faster, while
using the SB the endresult is no constant)

I assume that Line1 and Line2 are in normal live Strings and no constants.
(I tested it a short while ago the test should be in this newsgroup)

Cor
 
Hi Jay,

I did not copy it I was sending it when I saw it . However that you would
not think of me, I know.

:-)

Cor
 
Cor,
Huh?

Don't worry, I think of you.

Or maybe you should worry that I think of you. ;-)

Jay
 
* "Cor Ligthert said:
You can choose for #1, however #2 is faster.
No!

(Execpt of course that you want to do as what Herfried says and use the
Stringbuilder to make this constant Dim myconst as String = "Line1" &
VbCrlF & "Line2" & VbCrlF & "Line3" & VbCrlF, than is this faster, while
using the SB the endresult is no constant)

No, I didn't say that. The compiler determines that '"Line2" & vbCrLf'
/is/ one constant and stores it as "Line2\r\n" in the binary.
 
Herfried,

I checked it out, it is a thread I like to keep in the dark.

:-)

Because I always was using
sb.append x
sb.append y
and suddenly saw that it was not so much difference with
sb.append x & y

I started to give advises with the last one.

Somebodey else said that the last one was not efficient so I said that I had
checked it, and there was not much difference (My idea was that it had no
extra value to make it more difficult to describe for an OP who never has
used the SB)

The OP checked it out and the first methode that I show here is faster.

Now I think that I probably I have once tested it with constants when I
concatenated it.

Cor
 
* "Cor Ligthert said:
Yes

(When it are not *constants* Google this newsgroup than you find probably a
test I made).

But we were talking about constants ;-).
 
Herfried,
But we were talking about constants ;-).

Yes and therefore was this, sentence from me.

Because you took the sample really concrete.

My sentence in that message was not the worsest this year however absolute
not the best either. (It were first 3 rows) so maybe this is better to
understand.

What I did want to say was, that you better cannot use with this sample a
stringbuilder, but direct a dim statement, because as you showed it will
than be one constant and not a builded string object.

Cor
 
Jay,
Thanks for the excellent advice (as usual <g>).

I didn't mean to start a war here!

I like #1 for its "code readability".
I am glad to hear it does not *really* matter since there is no loop
involved with thousands of concatenations.

Imagine that the string "constants" are the clauses of a SQL statement and
you can see why #1 is more readable.
Thanks all!

--
Joe Fallon
Access MVP
 
Imagine that the string "constants" are the clauses of a SQL statement

Now that is opening a whole 'nuther can of worms. :^)

Greg
 
Joe,
Imagine that the string "constants" are the clauses of a SQL statement and
you can see why #1 is more readable.
For SQL statements I would seriously consider using a parameterized SQL
statement, rather then building one dynamically, as building one can lead to
SQL Injection attacks!

Depending on the needs of the app, I would consider dynamically building a
parameterized SQL statement.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top