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
15 1604
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" <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).
But we were talking about constants ;-).
--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Herfried, But we were talking about constants ;-).
Yes and therefore was this, sentence from me. (Execpt of course that you want to do as what Herfried says
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
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> schreef in bericht
news:e6******** ******@TK2MSFTN GP10.phx.gbl... * "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).
But we were talking about constants ;-).
-- Herfried K. Wagner [MVP] <URL:http://dotnet.mvps.org/> <URL:http://dotnet.mvps.org/dotnet/faqs/>
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
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:%2******** **********@TK2M SFTNGP12.phx.gb l... 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
> 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
"Joe Fallon" <jf******@nospa mtwcny.rr.com> wrote in message
news:uT******** ********@TK2MSF TNGP09.phx.gbl. .. 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 "Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message news:%2******** **********@TK2M SFTNGP12.phx.gb l... 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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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,
|
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);
|
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
|
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.
|
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.
...
| |
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
|
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...
|
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
|
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....
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
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();...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |