Re: string concatenation efficiency
- From: "MikeD" <nobody@xxxxxxxxxxx>
- Date: Tue, 25 Oct 2005 22:49:56 -0400
"MP" <nospam@xxxxxxxxxx> wrote in message
news:yhB7f.18415$1A1.4275@xxxxxxxxxxxxxxxxxxxxxxxx
> Hi,
> There is always much discussion on the inefficiency of vb string
> concatenation.
> The following is adapted from a recent post on this by Mike Williams.
>
> I often concatenate strings in multiple locations through a project to
> build
> a report string for debugging purposes.
> on error or at end of program the string is written to file and the file
> opened to see what transpired.
>
I'm probably going to get some flack over this, but here's my take.
Yes, string concatenation (actually, string usage in general) is not highly
efficient in VB. String concatenation is one of the most inefficient things
VB does and it's definitely something you need to keep in mind and give it
due consideration. However, unless your app requires the absolute utmost
efficiency you can possibly achieve, *don't worry too much about it*.
I'm not saying to do "stupid" things, like concatenating a bunch of literal
strings, as such:
sFileName = "C:\MyFolder" & "\" & "MySubfolder" & "\" & "filename" & "." &
"ext"
Now that's a slight exaggeration, but you see that kind of thing done far to
often (building inline SQL statements might have even been a better example,
but I wanted to use something more familiar to the masses).
What I AM saying is don't spend days or weeks writing and re-writing a ton
of code to improve whatever string concatenation you need to do (and
probably only see marginal improvements, at least to a point that it'd be
noticeable to the end user).
Bottom line is just use some common sense (and a few simple tips on how to
work as efficiently as possible with strings helps too). Most likely, your
time and effort will be better spent on other aspects of your program.
Here's a tip in which you can instantly improve string usage, efficiency,
and performance: It's common to have to check if a string is 0-length.
Rather than do it this way:
If MyString = "" Then
do it this way:
If Len(MyString) = 0 Then
VB can get the length of a string extremely efficiently (and as a bonus,
you're comparing 2 integers). While I've never done benchmark tests or
anything to show it's more efficient, I also use the StrComp function
whenever possible to compare 2 strings.
--
Mike
Microsoft MVP Visual Basic
.
- Prev by Date: Re: string concatenation efficiency
- Next by Date: Re: string concatenation efficiency
- Previous by thread: Re: string concatenation efficiency
- Next by thread: Re: string concatenation efficiency
- Index(es):
Relevant Pages
|