Re: string concatenation efficiency

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"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


.



Relevant Pages

  • Re: syntax...
    ... B&D on the part of the language designer. ... probably handle concatenation of string literals by itself, ... bitwise XOR, or if not that, then exponentiation.) ...
    (comp.lang.misc)
  • Bugs in the Module::Dependency
    ... Manifying blib/man1/pmd_indexer.plx.1 ... Use of uninitialized value in concatenation or string at ... # Failed test in t/04grapher.t at line 81. ...
    (perl.dbi.users)
  • RE: Simple regex problem has me baffled
    ... I am writing a script to analyse a log file. ... Each pair of Request and Response calls have a unique ... Use of uninitialized value in concatenation or string at ...
    (perl.beginners)
  • Re: Literal concatenation, strings vs. numbers (was: Numeric literals in other than base 10 - wa
    ... Well my take on it is that this would not be the same as string ... concatenation, the series of digits would be parsed as a single token ... digit separation can superficially resemble string concatenation if ... I would favor _ as a digit separator in Python ...
    (comp.lang.python)
  • Re: Simple regex problem has me baffled
    ... I am writing a script to analyse a log file. ... Each pair of Request and Response calls have a unique ... Use of uninitialized value in concatenation or string at ./magic.pl ...
    (perl.beginners)