473,769 Members | 6,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stringbuilder and concatentaion question

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(vbCrL f)
sb.Append("Line 2")
sb.Append(vbCrL f)
sb.Append("Line 3")
sb.Append(vbCrL f)

--
Joe Fallon

Nov 20 '05 #1
15 1603
* "Joe Fallon" <jf******@nospa mtwcny.rr.com> scripsit:
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(vbCrL f)
sb.Append("Line 2")
sb.Append(vbCrL f)
sb.Append("Line 3")
sb.Append(vbCrL f)


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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #2
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.
Nov 20 '05 #3
Herfried,
Excellent.
I was hoping for an answer like that!
Thanks.
--
Joe Fallon
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:OP******** ******@TK2MSFTN GP12.phx.gbl...
* "Joe Fallon" <jf******@nospa mtwcny.rr.com> scripsit:
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(vbCrL f)
sb.Append("Line 2")
sb.Append(vbCrL f)
sb.Append("Line 3")
sb.Append(vbCrL f)


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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 20 '05 #4
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(f ormat, line1, line2, line3)
End Sub

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

http://msdn.microsoft.com/architectu...l/scalenet.asp

Hope this helps
Jay

"Joe Fallon" <jf******@nospa mtwcny.rr.com> wrote in message
news:eY******** ******@TK2MSFTN GP10.phx.gbl... 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(vbCrL f)
sb.Append("Line 2")
sb.Append(vbCrL f)
sb.Append("Line 3")
sb.Append(vbCrL f)

--
Joe Fallon

Nov 20 '05 #5
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
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(vbCrL f)
sb.Append("Line 2")
sb.Append(vbCrL f)
sb.Append("Line 3")
sb.Append(vbCrL f)

--
Joe Fallon

Nov 20 '05 #6
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

Nov 20 '05 #7
Cor,
Huh?

Don't worry, I think of you.

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

Jay

"Cor Ligthert" <no**********@p lanet.nl> wrote in message
news:Ou******** ******@tk2msftn gp13.phx.gbl...
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

Nov 20 '05 #8
* "Cor Ligthert" <no**********@p lanet.nl> scripsit:
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 K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #9
> * "Cor Ligthert" <no**********@p lanet.nl> scripsit:
You can choose for #1, however #2 is faster.


No!

Yes

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

Cor
Nov 20 '05 #10

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

Similar topics

37
4719
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, 20-30k), what are the performance differences (if any with something as trivial as this) between initializing a new instance of StringBuilder with a specified capacity vs. initializing a new instance without... (the final length is not fixed) ie,
2
7393
by: Peter | last post by:
Hi, A newbie question .. I want to use an array of length 4 while each array element is a string of 40 chars. I typed .. StringBuilder title = new StringBuilder(40);
5
8496
by: Christof Nordiek | last post by:
Hi, in my Application i have to make a Copy of a StringBuilder, so that, if the new old instance is changed, the you instance will not be affected. I could do something like new StringBuilder(myStringBuilder.ToString()) but as i understand correct this would twice copy the content of my
9
9158
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a StringBuilder. At present I am porting a VB6 webclass app to VB.NET and therefore I am trying to make it as efficent as possible via the new functionality of VB.NET.
15
2814
by: DV | last post by:
I have a StringBuilder that has a string with 12,000,000 characters. When I do a ToString(), I expect to have ~25,000,000 bytes worth of memory, yet, I end up with ~43,000,000 bytes. That's almost double the size. The string returned from ToString() is actually of size StringBuilder.Capacity, NOT StringBuilder.Length. It may have a end-of-string character at StringBuilder.Length, but its actual memory size is StringBuilder.Capacity. ...
7
958
by: KH | last post by:
API question... Why doesn't StringBuilder have IndexOf and other similar methods like String? I can't think of a good reason off the top of my head. Easy to write helper functions to do the same thing, but sure would be helpful in certain cases. KH
12
2716
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 comparative analysis between the two and, surprisingly, string seems to out perform StringBuilder by a significant amount. A string concatenation takes not quite twice as long using StringBuilder than it does with a string. This doesn't sound right to...
26
3206
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: http://hardywang.1accesshost.com ICQ: 3359839 yours Hardy
3
8707
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 string length is larger than 16. An ArgumentOutOfRangeExceptoin is raised. However, if string is used, no exception. I searched this topic. It is said that StringBuilder is preferred in this case, because the performance is better. I don't think so....
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.